NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.37k stars 13.59k forks source link

NixOS: switch-to-configuration script does not correctly add a boot entry when executed standalone #82851

Open kaldonir opened 4 years ago

kaldonir commented 4 years ago

Describe the bug

Activating a NixOS configuration not through nixos-rebuild switch, but through ${(pkgs.nixos ./configuration.nix).toplevel}/bin/switch-to-configuration switch does activate the configuration correctly, but does not correctly add a boot entry.

The help text of the switch-to-configuration script states that switch should make this configuration the boot default.

The concrete symptoms differ between the different boot loaders:

To Reproduce Steps to reproduce the behavior on a UEFI system:

  1. cd /etc/nixos/
  2. Change something in configuration.nix
  3. sudo nix-build -E "with import <nixpkgs> { }; (nixos ./configuration.nix).toplevel"
  4. result/bin/switch-to-configuration switch
  5. Observe that the change has been applied
  6. Reboot
  7. Observe that the change has been reverted

Expected behavior The switch-to-configuration script should work in a standalone manner, and its help text should match its behaviour.

Additional context The grub and systemd boot scripts in nixos/modules/system/boot/loader/ assume that the target generation has been addded to /nix/var/nix/profiles/system. This happens when I run nixos-rebuild switch, but is not a part of the switch-to-configuration script.

A workaround is to run nix-env -p /nix/var/nix/profiles/system --set ./result before running switch-to-configuration.

Metadata Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

 - system: `"x86_64-linux"`
 - host os: `Linux 5.4.24, NixOS, 19.09pre-git (Loris)`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.3.3`
 - channels(root): `"nixos-19.09.2229.d7843c8add6"`
 - nixpkgs: `/nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs`

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute:
# a list of nixos modules affected by the problem
module:
kaldonir commented 4 years ago

I thought it is a feature.

If it is, is there any documentation about this? At least the command line documentation of switch-to-configuration doesn't mention this at all, it gives the impression that the script itself takes care of everything.

If switch-to-configuration will do nix-env -p ... it will breaks many things (nixops and similar software)

What exactly are the things that will break there? (Not trying to be snarky, trying to figure out what's the best way for finxing this issue [if there's something to fix at all])

flokli commented 4 years ago

@volth I also stumbled into this. I see both nixops and morph calling nix-env -p /nix/var/nix/profiles/system --set ./result, but I don't see a reason why it can't be part of switch-to-configuration (it should IMHO).

flokli commented 4 years ago

Also, cc @tilpner and https://logs.nix.samueldr.com/nixos/2020-03-16#3186041

stale[bot] commented 3 years ago

Hello, I'm a bot and I thank you in the name of the community for opening this issue.

To help our human contributors focus on the most-relevant reports, I check up on old issues to see if they're still relevant. This issue has had no activity for 180 days, and so I marked it as stale, but you can rest assured it will never be closed by a non-human.

The community would appreciate your effort in checking if the issue is still valid. If it isn't, please close it.

If the issue persists, and you'd like to remove the stale label, you simply need to leave a comment. Your comment can be as simple as "still important to me". If you'd like it to get more attention, you can ask for help by searching for maintainers and people that previously touched related code and @ mention them in a comment. You can use Git blame or GitHub's web interface on the relevant files to find them.

Lastly, you can always ask for help at our Discourse Forum or at #nixos' IRC channel.

bjornfor commented 3 years ago

Just found this issue, and this has been bugging me for months that ./result/bin/switch-to-configuration boot (and switch) does not configure the boot loader!

I looks like it was clearly intended to:

$ ./result/bin/switch-to-configuration --help
Usage: ./result/bin/switch-to-configuration [switch|boot|test]

switch:       make the configuration the boot default and activate now
boot:         make the configuration the boot default
test:         activate the configuration, but don't make it the boot default
dry-activate: show what would be done if this configuration were activated
stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

L-as commented 3 years ago

I don't think it's a wise decision to make switch-to-configuration mess with your system profiles. You have to consider that you can run the switch-to-configuration of any previous generation, and when you run it again, it shouldn't be come the latest generation. It should stay the latest generation.

Wouldn't making this behavior more clear in --help be enough? E.g. "make the profile with this configuration (if there is any) the boot default and active now"

kaldonir commented 3 years ago

Updating the help text could be enough to avoid confusion, but then there is still the issue that switch-to-configuration switch's exact behavior depends on the configured boot loader.

The help text could state the assumptions, i.e., the target generation has been added to /nix/var/nix/profiles/system, and that would definitely be better than what we currently have. But then, if switch-to-configuration can only be reasonably used by taking care of that beforehand, why not do it in the activation script itself?

nixos-discourse commented 2 years ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/proper-way-to-build-a-remote-system-with-flakes/17661/6

nixos-discourse commented 2 years ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/introducing-bento-a-nixos-deployment-framework/21446/16

nixos-discourse commented 1 year ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/tips-tricks-for-nixos-desktop/28488/5

bjornfor commented 11 months ago

I don't think it's a wise decision to make switch-to-configuration mess with your system profiles. You have to consider that you can run the switch-to-configuration of any previous generation, and when you run it again, it shouldn't be come the latest generation. It should stay the latest generation.

Isn't that what switch-to-configuration test is for? AFAICT, the switch-to-configuration help text is is good/right but the implementation is wrong.

nixos-discourse commented 10 months ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nixmaker-tool-to-convert-a-flake-to-a-tarball-per-each-nixos-config/34375/3

nikstur commented 10 months ago

Pulling this into switch-to-configuration would also make testing much much easier. Right now this functionality is effectively provided by nixos-rebuild which expects a "written" config somewhere which makes testing unnecessarily hard.

bjornfor commented 10 months ago

Related: https://github.com/NixOS/nixpkgs/issues/266290 ("NixOS apply script [developer oriented feature]")