Copy directory from custom iso to system after installation

I am building a custom iso for an unattended install of nixos. I would like to copy a directory of files into the iso and then copy it into the newly installed system. The files are encrypted secrets that I would manually decrypt and use to bootstrap a separate flake. It would act like a manual version of the --extra-files option in nixos-anywhere.

I have succeeded in adding the directory to the iso:

  isoImage.contents = [ { source = ../bootstrap ;
                          target = "/bootstrap";
                        } ];

But I don’t know how to copy the directory into the newly installed system.

I have been working with an earlier post by @misuzu and tried adding a cp command to the shell script in iso.nix but couldn’t work out how to reference the iso mount path as the source of the copy.

What would be the best way to copy a /bootstrap directory from the iso to the system during installation? Ideally I would copy it into my user’s home directory.

You don’t need to add anything to iso, add something like this to the configuration.nix instead:

system.activationScripts.mybootstrap.text = ''
  if [[ ! -e /bootstrap ]]; then
    cp -r ${../bootstrap} /bootstrap
  fi
'';

Thanks very much, that works and it’s simpler than setting the contents of the iso.

Just so I understand this, it appears to copy the bootstrap directory into the nix store on the iso, and then copies it out into the rebuilt system as part of the install. Is that correct?

Is that how nixos would usually work with scripts involving files, add them to the nix store before operating on them? This area is new to me.

1 Like

Yes, it’s a feature of a path type: https://nix.dev/manual/nix/2.18/language/values#type-path

2 Likes