diff --git a/dotfiles/README.md b/dotfiles/README.md index 435d366..be174c2 100644 --- a/dotfiles/README.md +++ b/dotfiles/README.md @@ -9,7 +9,11 @@ tags: [helper] # Dotfiles -Allow developers to optionally bring their own [dotfiles repository](https://dotfiles.github.io)! Under the hood, this module uses the [coder dotfiles](https://coder.com/docs/v2/latest/dotfiles) command. +Allow developers to optionally bring their own [dotfiles repository](https://dotfiles.github.io). + +This will prompt the user for their dotfiles repository URL on template creation using a `coder_parameter`. + +Under the hood, this module uses the [coder dotfiles](https://coder.com/docs/v2/latest/dotfiles) command. ```tf module "dotfiles" { @@ -19,6 +23,47 @@ module "dotfiles" { } ``` +## Examples + +### Apply dotfiles as the current user + +```tf +module "dotfiles" { + source = "registry.coder.com/modules/dotfiles/coder" + version = "1.0.0" + agent_id = coder_agent.example.id +} +``` + +### Apply dotfiles as another user (only works if sudo is passwordless) + +```tf +module "dotfiles" { + source = "registry.coder.com/modules/dotfiles/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + user = "root" +} +``` + +### Apply the same dotfiles as the current user and root (the root dotfiles can only be applied if sudo is passwordless) + +```tf +module "dotfiles" { + source = "registry.coder.com/modules/dotfiles/coder" + version = "1.0.0" + agent_id = coder_agent.example.id +} + +module "dotfiles-root" { + source = "registry.coder.com/modules/dotfiles/coder" + version = "1.0.0" + agent_id = coder_agent.example.id + user = "root" + dotfiles_uri = module.dotfiles.dotfiles_uri +} +``` + ## Setting a default dotfiles repository You can set a default dotfiles repository for all users by setting the `default_dotfiles_uri` variable: diff --git a/dotfiles/main.tf b/dotfiles/main.tf index ac7f7e6..950cb9b 100644 --- a/dotfiles/main.tf +++ b/dotfiles/main.tf @@ -16,10 +16,23 @@ variable "agent_id" { variable "default_dotfiles_uri" { type = string - description = "The default dotfiles URI if the workspace user does not provide one." + description = "The default dotfiles URI if the workspace user does not provide one" default = "" } +variable "dotfiles_uri" { + type = string + description = "The URL to a dotfiles repository. (optional, when set, the user isn't prompted for their dotfiles)" + + default = null +} + +variable "user" { + type = string + description = "The name of the user to apply the dotfiles to. (optional, applies to the current user by default)" + default = null +} + variable "coder_parameter_order" { type = number description = "The order determines the position of a template parameter in the UI/CLI presentation. The lowest order is shown first and parameters with equal order are sorted by name (ascending order)." @@ -27,6 +40,8 @@ variable "coder_parameter_order" { } data "coder_parameter" "dotfiles_uri" { + count = var.dotfiles_uri == null ? 1 : 0 + type = "string" name = "dotfiles_uri" display_name = "Dotfiles URL (optional)" @@ -37,14 +52,17 @@ data "coder_parameter" "dotfiles_uri" { icon = "/icon/dotfiles.svg" } -resource "coder_script" "personalize" { - agent_id = var.agent_id - script = <<-EOT - DOTFILES_URI="${data.coder_parameter.dotfiles_uri.value}" - if [ -n "$${DOTFILES_URI// }" ]; then - coder dotfiles "$DOTFILES_URI" -y 2>&1 | tee -a ~/.dotfiles.log - fi - EOT +locals { + dotfiles_uri = var.dotfiles_uri != null ? var.dotfiles_uri : data.coder_parameter.dotfiles_uri[0].value + user = var.user != null ? var.user : "" +} + +resource "coder_script" "dotfiles" { + agent_id = var.agent_id + script = templatefile("${path.module}/run.sh", { + DOTFILES_URI : local.dotfiles_uri, + DOTFILES_USER : local.user + }) display_name = "Dotfiles" icon = "/icon/dotfiles.svg" run_on_start = true @@ -52,5 +70,5 @@ resource "coder_script" "personalize" { output "dotfiles_uri" { description = "Dotfiles URI" - value = data.coder_parameter.dotfiles_uri.value + value = local.dotfiles_uri } diff --git a/dotfiles/run.sh b/dotfiles/run.sh new file mode 100644 index 0000000..9463439 --- /dev/null +++ b/dotfiles/run.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +DOTFILES_URI="${DOTFILES_URI}" +DOTFILES_USER="${DOTFILES_USER}" + +if [ -n "$${DOTFILES_URI// }" ]; then + if [ -z "$DOTFILES_USER" ]; then + DOTFILES_USER="$USER" + fi + + echo "✨ Applying dotfiles for user $DOTFILES_USER" + + if [ "$DOTFILES_USER" = "$USER" ]; then + coder dotfiles "$DOTFILES_URI" -y 2>&1 | tee ~/.dotfiles.log + else + # The `eval echo ~"$DOTFILES_USER"` part is used to dynamically get the home directory of the user, see https://superuser.com/a/484280 + # eval echo ~coder -> "/home/coder" + # eval echo ~root -> "/root" + + CODER_BIN=$(which coder) + DOTFILES_USER_HOME=$(eval echo ~"$DOTFILES_USER") + sudo -u "$DOTFILES_USER" sh -c "'$CODER_BIN' dotfiles '$DOTFILES_URI' -y 2>&1 | tee '$DOTFILES_USER_HOME'/.dotfiles.log" + fi +fi