Can't run the switch-to-configuration command?

I am getting errors like this:

building the system configuration...
Copied "/nix/store/73l80gfxnnchah2dzd97w42dc71dv4vx-systemd-257.6/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/EFI/systemd/systemd-bootx64.efi".
Copied "/nix/store/73l80gfxnnchah2dzd97w42dc71dv4vx-systemd-257.6/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/EFI/BOOT/BOOTX64.EFI".
⚠️ Mount point '/boot' which backs the random seed file is world accessible, which is a security hole! ⚠️
⚠️ Random seed file '/boot/loader/random-seed' is world accessible, which is a security hole! ⚠️
Random seed file /boot/loader/random-seed successfully refreshed (32 bytes).
Created EFI boot entry "Linux Boot Manager".
failed to synthesize: failed to read /nix/store/22i3lzk6b9vxs2ifxp1lap1wh907z1b7-user-environment/nixos-version: No such file or directory (os error 2)
Traceback (most recent call last):
  File "/nix/store/miw88xwkbkip8p715sm1k6kc9x02i2nw-systemd-boot/bin/systemd-boot", line 431, in <module>
    main()
    ~~~~^^
  File "/nix/store/miw88xwkbkip8p715sm1k6kc9x02i2nw-systemd-boot/bin/systemd-boot", line 414, in main
    install_bootloader(args)
    ~~~~~~~~~~~~~~~~~~^^^^^^
  File "/nix/store/miw88xwkbkip8p715sm1k6kc9x02i2nw-systemd-boot/bin/systemd-boot", line 361, in install_bootloader
    remove_old_entries(gens)
    ~~~~~~~~~~~~~~~~~~^^^^^^
  File "/nix/store/miw88xwkbkip8p715sm1k6kc9x02i2nw-systemd-boot/bin/systemd-boot", line 243, in remove_old_entries
    bootspec = get_bootspec(gen.profile, gen.generation)
  File "/nix/store/miw88xwkbkip8p715sm1k6kc9x02i2nw-systemd-boot/bin/systemd-boot", line 124, in get_bootspec
    boot_json_str = run(
                    ~~~^
        [
        ^
    ...<6 lines>...
        stdout=subprocess.PIPE,
        ^^^^^^^^^^^^^^^^^^^^^^^
    ).stdout
    ^
  File "/nix/store/miw88xwkbkip8p715sm1k6kc9x02i2nw-systemd-boot/bin/systemd-boot", line 58, in run
    return subprocess.run(cmd, check=True, text=True, stdout=stdout)
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/sd81bvmch7njdpwx3lkjslixcbj5mivz-python3-3.13.4/lib/python3.13/subprocess.py", line 577, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/nix/store/wpbfvilnaw00cxj61vsg74i0b106g3cb-bootspec-1.0.1/bin/synthesize', '--version', '1', '/nix/var/nix/profiles/system-207-link', '/dev/stdout']' returned non-zero exit status 1.
Failed to install bootloader
Command '['systemd-run', '-E', 'LOCALE_ARCHIVE', '-E', 'NIXOS_INSTALL_BOOTLOADER', '--collect', '--no-ask-password', '--pipe', '--quiet', '--service-type=exec', '--unit=nixos-rebuild-switch-to-configuration', PosixPath('/nix/store/p0zs8r46svd7i2livbfq0s7a2glxyahy-nixos-system-nixos-25.11.20250619.08f2208/bin/switch-to-configuration'), 'switch']' returned non-zero exit status 1.

In this one ^, I also passed the --boot-loader flag just to see. I am not sure how to address this or what caused it.

If I do sudo nixos-rebuild build --show-trace --flake nixfiles/#sour, it gives me this error that inidcates it can’t run the build command either:

building the system configuration...
error: flake 'git+file:///home/sour/nixfiles' does not provide attribute 'packages.x86_64-linux.nixosConfigurations."sour".config.system.build.toplevel', 'legacyPackages.x86_64-linux.nixosConfigurations."sour".config.system.build.toplevel' or 'nixosConfigurations."sour".config.system.build.toplevel'
Command '['nix', '--extra-experimental-features', 'nix-command flakes', 'build', '--print-out-paths', 'git+file:///home/sour/nixfiles#nixosConfigurations."sour".config.system.build.toplevel', '--show-trace']' returned non-zero exit status 1.

Looks like you ran nixos-rebuild with the pwd in the nix store. There’s a very strange bug in nix
which causes it to create a link with the wrong contents in this case. Compounded with this issue with the systemd-boot menu generation script, you get this behavior.

Check the links of the form /nix/var/nix/profiles/system-*-link. This is what they look like on a healthy system:

$ ls -l /nix/var/nix/profiles/system-*-link
lrwxrwxrwx 1 root root 90 May 21 23:41 /nix/var/nix/profiles/system-692-link -> /nix/store/w9ylm4ngkwkhsg9jn7wlrbpdhs3zhg3i-nixos-system-tejingdesk-24.11.20241210.a0f3e10
lrwxrwxrwx 1 root root 90 May 27 05:12 /nix/var/nix/profiles/system-693-link -> /nix/store/gnmcxx4wz7g16gr3y8hdrmbhnl40zx44-nixos-system-tejingdesk-24.11.20241210.a0f3e10
lrwxrwxrwx 1 root root 90 May 28 05:56 /nix/var/nix/profiles/system-694-link -> /nix/store/slfjyywqkfx47njqrmdm3pgm6wqrqsva-nixos-system-tejingdesk-24.11.20241210.a0f3e10
lrwxrwxrwx 1 root root 90 Jun  5 17:13 /nix/var/nix/profiles/system-695-link -> /nix/store/fsdv2mc0xnxq83zs4mrdf5s2qhxhkbp5-nixos-system-tejingdesk-24.11.20241210.a0f3e10
lrwxrwxrwx 1 root root 90 Jun 11 21:43 /nix/var/nix/profiles/system-696-link -> /nix/store/hnhkx3r3wz5zgydpag8vrv35a74rwcv6-nixos-system-tejingdesk-24.11.20241210.a0f3e10
lrwxrwxrwx 1 root root 90 Jun 13 07:32 /nix/var/nix/profiles/system-697-link -> /nix/store/x60avy64p2vmsl05aj60scp5x3cpfhmp-nixos-system-tejingdesk-24.11.20241210.a0f3e10
lrwxrwxrwx 1 root root 90 Jun 15 00:13 /nix/var/nix/profiles/system-698-link -> /nix/store/cr7ab411k62clwp3y6gsqlzwn4flbwzi-nixos-system-tejingdesk-24.11.20241210.a0f3e10
lrwxrwxrwx 1 root root 90 Jun 24 21:25 /nix/var/nix/profiles/system-699-link -> /nix/store/nzpj3l9kzmayk3rgnm1mpgc4lmf65k71-nixos-system-tejingdesk-25.05.20250622.c7ab752
lrwxrwxrwx 1 root root 90 Jun 24 21:49 /nix/var/nix/profiles/system-700-link -> /nix/store/k9rxkng0x9a9ghvfmys8mmqp3xgcvnj3-nixos-system-tejingdesk-25.05.20250622.c7ab752

But you likely have 1 or more links that point to store paths ending in something very different. (not nixos-system-<something>) Delete those generation numbers and try rebuilding again. The error should go away.

As for the other command, are you sure you’re using a flake and that you have a configuration for the hostname sour? The following error only happens if your flake.nix just lacks that.

Oh, right, thank you. No, I do not have a flake called sour. It’s called default lol

With that fixed, my error is thus:

building the system configuration...
error: cannot find flake 'flake:nixfiles' in the flake registries
Command '['nix', '--extra-experimental-features', 'nix-command flakes', 'build', '--print-out-paths', 'nixfiles#nixosConfigurations."default".config.system.build.toplevel', '--show-trace']' returned non-zero exit status 1.

Try:

sudo nixos-rebuild build --show-trace --flake ./nixfiles#default

Also, --show-trace almost never actually helps, I would cut it from my commands.

Delete them with garbage collector or delete them with sudo rm? Just checking before I do either.

EDIT: It occurs to me that I cannot sudo rm because the nix store is read-only lol

Delete the links, not what they point to. You could do it with sudo rm, or you could use sudo nix-env -p /nix/var/nix/profiles/system --delete-generations <number>

(Or you could obliterate all previous generations of all profiles with sudo nix-collect-garbage -d. It’s overkill, but it would work as long as the bad generations are no longer the most recent.)