Need help on failure of building my configuration

I got a confusing error after doing sudo nixos-rebuild switch for my flake-based configuration.

Click to expand
building the system configuration...
Traceback (most recent call last):
  File "/nix/store/w9wk3m6aczb3v9nwj775n269ziw29z9b-systemd-boot", line 278, in main
    write_entry(*gen, machine_id)
  File "/nix/store/w9wk3m6aczb3v9nwj775n269ziw29z9b-systemd-boot", line 116, in write_entry
    kernel = copy_from_profile(profile, generation, specialisation, "kernel")
  File "/nix/store/w9wk3m6aczb3v9nwj775n269ziw29z9b-systemd-boot", line 90, in copy_from_profile
    copy_if_not_exists(store_file_path, "/boot%s" % (efi_file_path))
  File "/nix/store/w9wk3m6aczb3v9nwj775n269ziw29z9b-systemd-boot", line 25, in copy_if_not_exists
    shutil.copyfile(source, dest)
  File "/nix/store/k0z9n599k02hab8qjjp3ljw065iwjcvg-python3-3.9.6/lib/python3.9/shutil.py", line 264, in copyfile
    with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: '/nix/store/a1ycypk68q92m30dngkvk3wjrf68b71d-apostrophe-2.5/kernel'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/nix/store/w9wk3m6aczb3v9nwj775n269ziw29z9b-systemd-boot", line 314, in <module>
    main()
  File "/nix/store/w9wk3m6aczb3v9nwj775n269ziw29z9b-systemd-boot", line 284, in main
    print("ignoring profile '{}' in the list of boot entries because of the following error:\n{}".format(profile, e), file=sys.stderr)
UnboundLocalError: local variable 'profile' referenced before assignment
warning: error(s) occurred while switching to the new configuration

I’m running (Nix) 2.6.0pre20211217_6e6e998.

This error is very weird and it sounds like something in Nix’s state or store is in an unexpected configuration. Apostrophe is only specified in my home configuration (managed with home-manager), not my system configuration.
I previously tried building an alternate version of Apostrophe via nix-repl, and was somewhat surprised by the behaviour. I imported my /etc/nixos flake and then used pkgs = inputs.nixos.legacyPackages.x86_64-linux, and was surprised when autocompleting “legacyPackages” caused a (re?-)download of nixpkgs. Maybe something was mistakenly overridden.

I’ve run nix doctor to no avail.

You can find my NixOS configuration here: GitHub - schuelermine/configuration
You can find my home-manager configuration here: GitHub - schuelermine/home-manager-configuration

Please do not suggest upgrading to the newest nixpkgs and rebuilding everything, I doubt that’ll help and it’s not an option in my current situation because my internet connection at my current location is metered and I can’t do an 11GiB download. Regardless, it’d be nice to know where this error is coming from…

This is the second time I’ve seen something similar in the past couple days; the other time was with someone on matrix, with a different package than apostrophe

I think somehow you’ve ended up with a package other than your system configuration set as your system profile (readlink -f /nix/var/nix/profiles/system). I’m not sure how this is happening. Are you sure that nix show-derivation .#nixosConfigurations.HOSTNAME.config.system.build.toplevel is a nixos config?

EDIT: Here’s the person on matrix discovering this problem.

/etc/nixos#nixosConfigurations.buggeryyacht-nixos.config.system.build.toplevel is «derivation /nix/store/aaplprbz9a0gq43k9dp4wdb8c84vyp6m-nixos-system-buggeryyacht-nixos-22.05.20211225.cb372c3.drv».

anselmschueler@buggeryyacht-nixos /n/s/7np998x1qv79pm0l8zpwm1b35fm6dk8f-nixos-system-buggeryyacht-nixos-22.05.20211225.cb372c3> pwd
/nix/store/7np998x1qv79pm0l8zpwm1b35fm6dk8f-nixos-system-buggeryyacht-nixos-22.05.20211225.cb372c3
anselmschueler@buggeryyacht-nixos /n/s/7np998x1qv79pm0l8zpwm1b35fm6dk8f-nixos-system-buggeryyacht-nixos-22.05.20211225.cb372c3> ls
activate               bin                 dry-activate  extra-dependencies  init                    initrd  kernel-modules  nixos-version   sw      systemd
append-initrd-secrets  configuration-name  etc           firmware            init-interface-version  kernel  kernel-params   specialisation  system  
anselmschueler@buggeryyacht-nixos /n/s/7np998x1qv79pm0l8zpwm1b35fm6dk8f-nixos-system-buggeryyacht-nixos-22.05.20211225.cb372c3> 

Hmm, I tried running the config’s ./activate… I didn’t get an error. Maybe something is wrong with this version of nixos-rebuild?

Note, you’re probably supposed to run bin/switch-to-configuration switch, not activate :stuck_out_tongue: Anway, yea I think there must be some bug in nixos-rebuild that causes it to sometimes set the system profile to the wrong thing. Doesn’t really make sense, but that’s all I can think of.

EDIT: Also note that just switching to the configuration won’t fix the system profile pointing to the wrong thing. readlink -f /nix/var/nix/profiles/system to verify it’s pointing to something wrong, then rollback the generation and delete the bad one.

1 Like

…but it only spat out setting up /etc...

That would be because of the switch-to-configuration thing. Activation is only a subset of switch-to-configuration.

I see. I tried ./bin/switch-to-configuration and got the same error. Why is it trying to do anything with /nix/store/a1ycypk68q92m30dngkvk3wjrf68b71d-apostrophe-2.5/kernel?

Also, the error

Traceback (most recent call last):
  File "/nix/store/w9wk3m6aczb3v9nwj775n269ziw29z9b-systemd-boot", line 314, in <module>
    main()
  File "/nix/store/w9wk3m6aczb3v9nwj775n269ziw29z9b-systemd-boot", line 284, in main
    print("ignoring profile '{}' in the list of boot entries because of the following error:\n{}".format(profile, e), file=sys.stderr)
UnboundLocalError: local variable 'profile' referenced before assignment

is concerning…

I think what happens is that the systemd-boot installation script goes through all the generations of the system profile and configures boot entries for them. So if any generation doesn’t point to a nixos configuration, then it’ll error because e.g. there’s no kernel in the output. Then this just cascades to other error messages like the local variable 'profile' one.

All that is to say: You need to delete the generation that is pointing to something that isn’t a nixos configuration. You can use readlink /nix/var/nix/profiles/system-* to figure out which one it is.

1 Like

That sounds plausible. I just tried doing switch-to-configuration test and it worked fine.

Right, because that doesn’t run the systemd-boot installation stuff.

OK, yeah, that’s what it is.

anselmschueler@buggeryyacht-nixos /e/nixos (b0) [1]> readlink /nix/var/nix/profiles/system-*
/nix/store/6d8vqn0ihf9qan1fjngxa7hwz3nsfd2c-nixos-system-buggeryyacht-nixos-22.05.20211224.eac07ed
/nix/store/azks0jca0kn0ykmrfbj6an50vp8qldmf-nixos-system-buggeryyacht-nixos-22.05.20211225.cb372c3
/nix/store/7np998x1qv79pm0l8zpwm1b35fm6dk8f-nixos-system-buggeryyacht-nixos-22.05.20211225.cb372c3
/nix/store/a1ycypk68q92m30dngkvk3wjrf68b71d-apostrophe-2.5
/nix/store/7np998x1qv79pm0l8zpwm1b35fm6dk8f-nixos-system-buggeryyacht-nixos-22.05.20211225.cb372c3

I’m guessing nix-repl building apostrophe somehow managed to replace that symlink? Weird…

I really wish I knew what was causing these spurious false generations. As I said, this is not the first time I’ve seen this in the past few days…

That seems odd… You did build apostrophe in the repl at some point though?

Maybe it has to do with flakes, I got an unexpected behaviour with them shortly before building apostrophe. I imported my /etc/nixos flake and tried getting the nixpkgs input from it, and, when autocompleting, nix repl for some reason re-downloaded nixpkgs, even though it seems like it should be available

maybe that’s normal though—does the GC remove inputs to non-GC’ed flakes?

OK, I removed old generations with nix-collect-garbage --delete-old. Now it works.