With nix-build, how do I copy configuration.nix into the system to be able to update it?

I’m following this tutorial to build an iso image:


, but the final image doesn’t have an /etc/nixos/configuration.nix

Is there an option to copy this with it, as well?

ok, so I can just include this?

  system.copySystemConfiguration = true;

I ran into the same issue recently. I did not have much luck with copySystemConfiguration.

I was able to find in in sd-image.nix an option for populateRootCommands. There is a default for this in the raspberry pi configuration and if you end up overwriting it you’ll probably want to include what is in the default as well. I’m not sure how important it is, though.

I now use something like the following to copy in the configuration nix along with a root ssh key:

    sdImage.populateRootCommands = ''
      # Copy in ssh keys
      mkdir -p ./files/etc/ssh/
      cp ./config/secrets/${targetName}.pub ./files/etc/ssh/ssh_host_ed25519_key.pub
      cp ./config/secrets/${targetName} ./files/etc/ssh/ssh_host_ed25519_key

      # Copy in configuration
      mkdir -p ./files/etc/nixos/
      cp ./config/configuration.nix ./files/etc/nixos/

      # Retain defaults for raspberry pi 
      mkdir -p ./files/boot
      ${config.boot.loader.generic-extlinux-compatible.populateCmd} -c ${config.system.build.toplevel} -d ./files/boot

Does this take imports into consideration?

I’m not exactly sure what you mean. The example I used above is just part of a standard nix config section, which typically also has imports, etc.

I mean, my configuration.nix is basically just imports, like

 imports =

, so basically, I would probably need to copy in all of them. I feel it’s a bit weird, but I guess that’s what needs to be done;).


That didn’t work;)

cp: cannot stat './config/configuration.nix': No such file or directory
error: builder for '/nix/store/aixf4yfdr93zzk03xb5icxq7q7saa8ay-ext4-fs.img.zst.drv' failed with exit code 1;
       last 1 log lines:
       > cp: cannot stat './config/configuration.nix': No such file or directory
       For full logs, run 'nix log /nix/store/aixf4yfdr93zzk03xb5icxq7q7saa8ay-ext4-fs.img.zst.drv'.
error: 1 dependencies of derivation '/nix/store/ak910xwmiakwcadqgx2j5z7gjm0r7khh-nixos-sd-image-23.05.3580.5d017a8822e0-aarch64-linux.img.drv' failed to build

I tried removing the “config” part of the path, but where is this supposed to be?

In the example you shared (pablo.tools blogpost) there is a sample configuration file rpi4-image.nix that contains a populateRootCommands section. You’d simply need to expand this to include copy commands for each file/folder you want to include.

The FROM side of the copy is wherever the config files live on your computer you are building from. The TO side of the copy is ./file/..., where … is the path you want to where you want the file to be in the resulting image (e.g. ./file/etc/nixos/configuration.nix).

If you need more help it would be good to share more detail of your set up, including file structure, commands you are using to build, etc.

Right, I will do that, but what is it relative to?

Here’s my addition to rpi-image.nix

 77     populateRootCommands = ''
 78      # Copy in configuration
 79       mkdir -p ./files/etc/nixos/
 80       cp ./config/pi400.configuration.nix ./files/etc/nixos/configuration.nix
 81       cp ./config/derivation.fundamental.nix ./files/etc/nixos/
 82       cp ./config/hydra.base.nix ./files/etc/nixos/
 83       cp ./config/hydra.locale.nix ./files/etc/nixos/
 84       cp ./config/hydra.network.nix ./files/etc/nixos/
 85       cp ./config/hydra.xorg.lightdm.kodi.nix ./files/etc/nixos/
 86       cp ./config/hydra.user.nix ./files/etc/nixos/
 88       mkdir -p ./files/boot
 89       ${config.boot.loader.generic-extlinux-compatible.populateCmd} -c ${config.system.build.toplevel} -d ./files/boot
 90     '';
 91   };

I even tried absolute path, like “/tmp/foo.nix” and just “./pi400.configuration.nix”

Here’s the build directory

quasar in nixos-raspi4-template שׂmain [!?] 6s
BASHY $ ls -lh
total 56K
-rw-r--r-- 1 b0ef users  195 Dec  1 20:28 base-config.nix
-rwxr-xr-x 1 b0ef users   92 Dec  1 20:28 build.sh
-rw-r--r-- 1 b0ef users  390 Dec 10 13:57 derivation.fundamental.nix
-rw-r--r-- 1 b0ef users  138 Dec 16 13:53 hydra.base.nix
-rw-r--r-- 1 b0ef users   90 Dec 10 14:21 hydra.locale.nix
-rw-r--r-- 1 b0ef users  298 Jan 17 23:07 hydra.network.nix
-rw-r--r-- 1 b0ef users 1.7K Dec 16 15:06 hydra.user.nix
-rw-r--r-- 1 b0ef users  615 Dec 16 15:09 hydra.xorg.lightdm.kodi.nix
-rw-r--r-- 1 b0ef users 1.1K Dec  1 20:28 LICENSE
-rw-r--r-- 1 b0ef users  355 Jan 17 23:01 pi400.configuration.nix
-rw-r--r-- 1 b0ef users  487 Dec  1 20:28 README.md
lrwxrwxrwx 1 b0ef users  100 Dec 15 22:54 result -> /nix/store/1dspk61aqdi0xdmkqbi04ydfsrriynwm-nixos-sd-image-23.05.3580.5d017a8822e0-aarch64-linux.img
lrwxrwxrwx 1 b0ef users  100 Dec  6 21:40 result-wtf -> /nix/store/mqg5v1fch4z4vfwfy2620b6p3yf4arbw-nixos-sd-image-23.05.3580.5d017a8822e0-aarch64-linux.img
-rw-r--r-- 1 b0ef users 3.4K Jan 17 23:31 rpi4-image.nix

I have the pi400.configuration.nix in the root of the build directory, so I would assume it would be “./pi400.configuration.nix”.

How come you pasted “./config/”? What is that relative to?

Relative to the current directly. I have my configs in a folder called config. In your case, looks like it’s a flat directory. So it should look like:

cp ./pi400.configuration.nix ./files/etc/nixos/configuration.nix

This is like totally bananas.

Here is an strace where it clearly reads the .nix file, as part of building it, but then it has problems copying it.


I’ve tried all sorts of renames and all sorts of subdirectories, but it’s not reading anything.