Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

feat(dotfiles): add ability to apply dotfiles as any user #133

Merged
merged 21 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d6b338c
feat(dotfiles): move script to run.sh
phorcys420 Jan 30, 2024
873f057
feat(dotfiles): add ability to provide an arbitrary dotfiles_uri
phorcys420 Jan 30, 2024
b8db92e
fix(dotfiles): remove non-existant `ROOT` variable from `run.sh`
phorcys420 Jan 30, 2024
8444934
refactor(dotfiles): move `dotfiles_uri` to local variables to avoid r…
phorcys420 Jan 30, 2024
e653503
feat(dotfiles): add ability to provide an arbitrary user
phorcys420 Jan 30, 2024
ae23a5f
fix(dotfiles): rename `coder_script` to "dotfiles" (from "personalize")
phorcys420 Jan 30, 2024
1d62eb7
chore(dotfiles): document the newly created `user` and `dotfiles_uri`…
phorcys420 Jan 30, 2024
bac5406
chore(dotfiles): clarify README to reflect that you can use any user
phorcys420 Mar 14, 2024
f44947f
chore(dotfiles): clarify README to reflect that root isn't the only o…
phorcys420 Mar 14, 2024
a2bdfef
chore(dotfiles): mention that setting the `dotfiles_uri` parameter di…
phorcys420 Mar 14, 2024
b27f396
fix(dotfiles): overwrite dotfiles log instead of appending
phorcys420 Mar 14, 2024
63c0712
fix(dotfiles): get the proper user's home directory
phorcys420 Mar 14, 2024
f7d9486
chore(dotfiles); fix comments in run.sh
phorcys420 Mar 14, 2024
6ffc79f
chore(dotfiles): clarify the script for other users
phorcys420 Apr 14, 2024
b9bec0f
Merge branch 'coder:main' into dotfiles-root
phorcys420 Apr 14, 2024
0b8fe33
fix(dotfiles): fix formatting
phorcys420 Apr 14, 2024
f94d0b7
Merge branch 'main' into dotfiles-root
phorcys420 Apr 14, 2024
808ee2b
fix(dotfiles): format & lint
phorcys420 Apr 14, 2024
437ea98
fix(dotfiles): fix typos
phorcys420 Apr 14, 2024
f92a612
chore(dotfiles): unfix typo in README to stay in the scope of the PR
phorcys420 Apr 14, 2024
9239a4c
Merge branch 'main' into dotfiles-root
matifali Apr 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion dotfiles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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:
Expand Down
38 changes: 28 additions & 10 deletions dotfiles/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,32 @@ 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
}
Comment on lines 17 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should only have one of these


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)."
default = null
}

data "coder_parameter" "dotfiles_uri" {
count = var.dotfiles_uri == null ? 1 : 0

type = "string"
name = "dotfiles_uri"
display_name = "Dotfiles URL (optional)"
Expand All @@ -37,20 +52,23 @@ 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
}

output "dotfiles_uri" {
description = "Dotfiles URI"
value = data.coder_parameter.dotfiles_uri.value
value = local.dotfiles_uri
}
23 changes: 23 additions & 0 deletions dotfiles/run.sh
Original file line number Diff line number Diff line change
@@ -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