Compile NixOS derivations into a single file for a single host

Ouh, thank you for testing!

I test this on AWS and that might be a problem.

on AWS NixOS instance

[root@ip-10-0-0-35:~]# nix --version
nix (Nix) 2.13.3

[root@ip-10-0-0-35:~]# realpath $(which nix)
/nix/store/j1vi2fl7liaxnp50an8h07py31pfjyhk-nix-2.13.3/bin/nix

on my laptop with the most current 23.05

nix (Nix) 2.13.5
/nix/store/0hpn8a73zrxfhxsv5b71ppfixm9dxx4x-nix-2.13.5/bin/nix

I’ll try to build it on my laptop or maybe via:

nix shell nixpkgs#nix

which gives me

nix --version
nix (Nix) 2.17.0

Ahh yeah that might be an issue. I’m curious to see your results! For reference, I used nix 2.15.1.

1 Like
  • on first
mkdir -p ~/.config/nix
tee ~/.config/nix/nix.conf <<< 'experimental-features = nix-command flakes'

nix shell nixpkgs#nix

nix --version

nix (Nix) 2.17.0

nix run nixpkgs#vim -- /etc/nixos/configuration.nix
nix run nixpkgs#vim -- /etc/nixos/flake.nix

nix build --no-link --print-out-paths /etc/nixos#nixosConfigurations.abc.config.system.build.toplevel

/nix/store/s38ck7liswgiskxh8smccywzkkdrixzl-nixos-system-unnamed-23.05.20231011.bd1cde4

nix-store -q --deriver /nix/store/s38ck7liswgiskxh8smccywzkkdrixzl-nixos-system-unnamed-23.05.20231011.bd1cde4

/nix/store/dmyyml7l7kbbcs6jvnckqrk5wldaq1f1-nixos-system-unnamed-23.05.20231011.bd1cde4.drv

nix copy /nix/store/dmyyml7l7kbbcs6jvnckqrk5wldaq1f1-nixos-system-unnamed-23.05.20231011.bd1cde4.drv --to file:///tmp/abc

tar -czf /tmp/abc.tar.gz -C /tmp abc

du -sh /tmp/abc.tar.gz /tmp/abc

6.6M    /tmp/abc.tar.gz
26M     /tmp/abc

nix store ls --store file:///tmp/abc /nix/store/dmyyml7l7kbbcs6jvnckqrk5wldaq1f1-nixos-system-unnamed-23.05.20231011.bd1cde4.drv

dmyyml7l7kbbcs6jvnckqrk5wldaq1f1-nixos-system-unnamed-23.05.20231011.bd1cde4.drv
  • on second
mkdir -p ~/.config/nix
tee ~/.config/nix/nix.conf <<< 'experimental-features = nix-command flakes'

nix shell nixpkgs#nix

nix --version

nix (Nix) 2.17.0

tar -xf /tmp/abc.tar.gz -C /tmp

ls -l /tmp/abc | grep narinfo | wc -l

2904

ls -l /tmp/abc/nar | grep nar.xz | wc -l

2904

nix copy /nix/store/dmyyml7l7kbbcs6jvnckqrk5wldaq1f1-nixos-system-unnamed-23.05.20231011.bd1cde4.drv --from file:///tmp/abc

warning: The interpretation of store paths arguments ending in `.drv` recently changed. If this command is now failing try again with '/nix/store/dmyyml7l7kbbcs6jvnckqrk5wldaq1f1-nixos-system-unnamed-23.05.20231011.bd1cde4.drv^*'

nix build "/nix/store/dmyyml7l7kbbcs6jvnckqrk5wldaq1f1-nixos-system-unnamed-23.05.20231011.bd1cde4.drv^*"

/nix/store/s38ck7liswgiskxh8smccywzkkdrixzl-nixos-system-unnamed-23.05.20231011.bd1cde4/bin/switch-to-configuration boot

updating GRUB 2 menu...

reboot

tmux

YEEEEEEEEEEEEEEEEEEEEEEEEES!!! tmux is there ;-). The config changed.

Thank you so much @iFreilicht !

4 Likes

Amazing! So glad to see it worked now! That was the longest debugging journey I’ve been through so far. Maybe you could mark my post with the instructions as the solution? I can add an edit that explains you need to check the nix version first.

Also glad to see that the archive size is reasonable now as well.

One final question: why were there old version of Nix on these EC2 instances? I thought you created them anew for testing this? Does Amazon host an old image of NixOS or something like that?

3 Likes

Thank you very much for your huge effort and time you spent on this!

Post marked as solution.

That was my fault since I have AMI (image) hardcoded in AWS CloudFormation that I use to create the VM. There are possibly more current images available.

1 Like

@honda Not sure if you’re following this thread, but we found the issue was an outdated version of nix. See the updated instructions for details.

2 Likes