|
| 1 | +# Copying files to the new installation |
| 2 | + |
| 3 | +The `--extra-files <path>` option allows copying files to the target host after |
| 4 | +installation. |
| 5 | + |
| 6 | +The contents of the `<path>` is recursively copied and overwrites the targets |
| 7 | +root (/). The contents _must_ be in a structure and permissioned as it should be |
| 8 | +on the target. |
| 9 | + |
| 10 | +In this way, there is no need to repeatedly pass arguments (eg: a fictional |
| 11 | +argument: `--copy <source> <dest>`) to `nixos-anywhere` to complete the intended |
| 12 | +outcome. |
| 13 | + |
| 14 | +The path and directory structure passed to `--extra-files` should be prepared |
| 15 | +beforehand. |
| 16 | + |
| 17 | +This allows a simple programmatic invocation of `nixos-anywhere` for multiple |
| 18 | +hosts. |
| 19 | + |
| 20 | +## Simple Example |
| 21 | + |
| 22 | +You want `/etc/ssh/ssh_host_*` and `/persist` from the local system on the |
| 23 | +target. The `<path>` contents will look like this: |
| 24 | + |
| 25 | +```console |
| 26 | +$ cd /tmp |
| 27 | +$ root=$(mktemp -d) |
| 28 | +$ sudo cp --verbose --archive --parents /etc/ssh/ssh_host_* ${root} |
| 29 | +$ cp --verbose --archive --link /persist ${root} |
| 30 | +``` |
| 31 | + |
| 32 | +The directory structure would look like this: |
| 33 | + |
| 34 | +```console |
| 35 | +drwx------ myuser1 users 20 tmp.d6nx5QUwPN |
| 36 | +drwxr-xr-x root root 6 ├── etc |
| 37 | +drwx------ myuser1 users 160 │ └── ssh |
| 38 | +.rw------- root root 399 │ ├── ssh_host_ed25519_key |
| 39 | +.rw-r--r-- root root 91 │ ├── ssh_host_ed25519_key.pub |
| 40 | +drwxr-xr-x myuser1 users 22 └── persist |
| 41 | +drwxr-xr-x myuser1 users 14 ├── all |
| 42 | +drwxr-xr-x myuser1 users 22 │ ├── my |
| 43 | +.rw-r--r-- myuser1 users 6 │ │ ├── test3 |
| 44 | +drwxr-xr-x myuser1 users 10 │ │ └── things |
| 45 | +.rw-r--r-- myuser1 users 6 │ │ └── test4 |
| 46 | +.rw-r--r-- myuser1 users 6 │ └── test2 |
| 47 | +drwxr-xr-x myuser1 users 0 ├── blah |
| 48 | +.rw-r--r-- myuser1 users 6 └── test |
| 49 | +``` |
| 50 | + |
| 51 | +**NOTE**: Permissions will be copied, but ownership on the target will be root. |
| 52 | + |
| 53 | +Then pass $root like: |
| 54 | + |
| 55 | +> nixos-anywhere --flake ".#" --extra-files $root --target-host root@newhost |
| 56 | +
|
| 57 | +## Programmatic Example |
| 58 | + |
| 59 | +```sh |
| 60 | +for host in host1 host2 host3; do |
| 61 | + root="target/${host}" |
| 62 | + install -d -m755 ${root}/etc/ssh |
| 63 | + ssh-keygen -A -C root@${host} -f ${root} |
| 64 | + nixos-anywhere --extra-files "${root}" --flake ".#${host}" --target-host "root@${host}" |
| 65 | +done |
| 66 | +``` |
| 67 | + |
| 68 | +## Considerations |
| 69 | + |
| 70 | +### Ownership |
| 71 | + |
| 72 | +The new system may have differing UNIX user and group id's for users created |
| 73 | +during installation. |
| 74 | + |
| 75 | +When the files are extracted on the remote the copied data will be owned by |
| 76 | +root. |
| 77 | + |
| 78 | +### Symbolic Links |
| 79 | + |
| 80 | +Do not create symbolic links to reference data to copy. |
| 81 | + |
| 82 | +GNU `tar` is used to do the copy over ssh. It is an archival tool used to |
| 83 | +re/store directory structures as is. Thus `tar` copies symbolic links created |
| 84 | +with `ln -s` by default. It does not follow them to copy the underlying file. |
| 85 | + |
| 86 | +### Hard links |
| 87 | + |
| 88 | +**NOTE**: hard links can only be created on the same filesystem. |
| 89 | + |
| 90 | +If you have larger peristent data to copy to the target. GNU `tar` will copy |
| 91 | +data referenced by hard links created with `ln`. A hard link does not create |
| 92 | +another copy the data. |
| 93 | + |
| 94 | +To copy a directory tree to the new target you can use the `cp` command with the |
| 95 | +`--link` option which creates hard links. |
| 96 | + |
| 97 | +#### Example |
| 98 | + |
| 99 | +```sh |
| 100 | +cd /tmp |
| 101 | +root=$(mktemp -d) |
| 102 | +cp --verbose --archive --link --parents /persist/home/myuser ${root} |
| 103 | +``` |
| 104 | + |
| 105 | +`--parents` will create the directory structure of the source at the |
| 106 | +destination. |
0 commit comments