Skip to content

Commit 7649236

Browse files
authored
Merge pull request #444 from costowell/main
add option to set ownership of extraFiles
2 parents a9ae344 + a4ab782 commit 7649236

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

docs/howtos/extra-files.md

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ during installation.
7575
When the files are extracted on the remote the copied data will be owned by
7676
root.
7777

78+
If you wish to change the ownership after the files are copied onto the system,
79+
you can use the `--chown` option.
80+
81+
For example, if you did `--chown /home/myuser/.ssh 1000:100`, this would equate
82+
to running `chown -R /home/myuser/.ssh 1000:100` where the uid is 1000 and the
83+
gid is 100. **Only do this when you can _guarantee_ what the uid and gid will
84+
be.**
85+
7886
### Symbolic Links
7987

8088
Do not create symbolic links to reference data to copy.

docs/reference.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ Options:
4949
copy over existing /etc/ssh/ssh_host_* host keys to the installation
5050
* --extra-files <path>
5151
contents of local <path> are recursively copied to the root (/) of the new NixOS installation. Existing files are overwritten
52-
Copied files will be owned by root. See documentation for details.
52+
Copied files will be owned by root unless specified by --chown option. See documentation for details.
53+
* --chown <path> <ownership>
54+
change ownership of <path> recursively. Recommended to use uid:gid as opposed to username:groupname for ownership.
55+
Option can be specified more than once.
5356
* --disk-encryption-keys <remote_path> <local_path>
5457
copy the contents of the file or pipe in local_path to remote_path in the installer environment,
5558
after kexec but before installation. Can be repeated.

src/nixos-anywhere.sh

+16-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ trap 'rm -rf "$sshKeyDir"' EXIT
6161
mkdir -p "$sshKeyDir"
6262

6363
declare -A diskEncryptionKeys=()
64+
declare -A extraFilesOwnership=()
6465
declare -a nixCopyOptions=()
6566
declare -a sshArgs=()
6667

@@ -103,7 +104,10 @@ Options:
103104
copy over existing /etc/ssh/ssh_host_* host keys to the installation
104105
* --extra-files <path>
105106
contents of local <path> are recursively copied to the root (/) of the new NixOS installation. Existing files are overwritten
106-
Copied files will be owned by root. See documentation for details.
107+
Copied files will be owned by root unless specified by --chown option. See documentation for details.
108+
* --chown <path> <ownership>
109+
change ownership of <path> recursively. Recommended to use uid:gid as opposed to username:groupname for ownership.
110+
Option can be specified more than once.
107111
* --disk-encryption-keys <remote_path> <local_path>
108112
copy the contents of the file or pipe in local_path to remote_path in the installer environment,
109113
after kexec but before installation. Can be repeated.
@@ -267,6 +271,11 @@ parseArgs() {
267271
extraFiles=$2
268272
shift
269273
;;
274+
--chown)
275+
extraFilesOwnership["$2"]="$3"
276+
shift
277+
shift
278+
;;
270279
--disk-encryption-keys)
271280
diskEncryptionKeys["$2"]="$3"
272281
shift
@@ -678,9 +687,15 @@ nixosInstall() {
678687
if [[ -n ${extraFiles} ]]; then
679688
step Copying extra files
680689
tar -C "$extraFiles" -cpf- . | runSsh "tar -C /mnt -xf- --no-same-owner"
690+
681691
runSsh "chmod 755 /mnt" # tar also changes permissions of /mnt
682692
fi
683693
694+
if [[ ${#extraFilesOwnership[@]} -gt 0 ]]; then
695+
# shellcheck disable=SC2016
696+
printf "%s\n" "${!extraFilesOwnership[@]}" "${extraFilesOwnership[@]}" | pr -2t | runSsh 'while read file ownership; do chown -R "$ownership" "/mnt/$file"; done'
697+
fi
698+
684699
step Installing NixOS
685700
runSsh sh <<SSH
686701
set -eu ${enableDebug}

tests/from-nixos.nix

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
start_all()
3535
installer.succeed("mkdir -p /tmp/extra-files/var/lib/secrets")
3636
installer.succeed("echo value > /tmp/extra-files/var/lib/secrets/key")
37+
installer.succeed("mkdir -p /tmp/extra-files/home/user/.ssh")
38+
installer.succeed("echo secretkey > /tmp/extra-files/home/user/.ssh/id_ed25519")
39+
installer.succeed("echo publickey > /tmp/extra-files/home/user/.ssh/id_ed25519.pub")
40+
installer.succeed("chmod 600 /tmp/extra-files/home/user/.ssh/id_ed25519")
3741
ssh_key_path = "/etc/ssh/ssh_host_ed25519_key.pub"
3842
ssh_key_output = installer.wait_until_succeeds(f"""
3943
ssh -i /root/.ssh/install_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
@@ -46,6 +50,7 @@
4650
--kexec /etc/nixos-anywhere/kexec-installer \
4751
--extra-files /tmp/extra-files \
4852
--store-paths /etc/nixos-anywhere/disko /etc/nixos-anywhere/system-to-install \
53+
--chown /home/user 1000:100 \
4954
--copy-host-keys \
5055
root@installed >&2
5156
""")
@@ -62,6 +67,10 @@
6267
assert "value" == content, f"secret does not have expected value: {content}"
6368
ssh_key_content = new_machine.succeed(f"cat {ssh_key_path}").strip()
6469
assert ssh_key_content in ssh_key_output, "SSH host identity changed"
70+
priv_key_perms = new_machine.succeed("stat -c %a /home/user/.ssh/id_ed25519").strip()
71+
assert priv_key_perms == "600", f"unexpected permissions for private key: {priv_key_perms}"
72+
user_dir_ownership = new_machine.succeed("stat -c %u:%g /home/user").strip()
73+
assert user_dir_ownership == "1000:100", f"unexpected user home dir permissions: {user_dir_ownership}"
6574
'';
6675
}
6776
)

0 commit comments

Comments
 (0)