Skip to content

Conversation

@Patryk27
Copy link
Member

@Patryk27 Patryk27 commented Dec 23, 2022

Description of changes

This commit fixes a papercut in nixos-rebuild where people wanting to switch to a specialisation (or test one) were forced to manually figure out the specialisation's path and run its activation script - since now, there's a dedicated option to do just that.

This is a backwards-compatible change which doesn't affect the existing behavior, which - to be fair - might still be considered sus by some people, the painful scenario here being:

  • you boot into specialisation foo,
  • you run nixos-rebuild switch,
  • whoops, you're no longer at specialisation foo, but you're rather brought back to the base system.

(it's especially painful for cases where specialisation is used to load extra drivers, e.g. Nvidia, since then launching nixos-rebuild switch, while forgetting that you're inside a specialisation, can cause some parts of your system to get accidentally unloaded.)

I've tried to mitigate that by improving specialisations so that they create a dedicated file somewhere in /run/current-system containing the specialisation's name (which nixos-rebuild could then use as the default value for --specialisation), but I haven't been able to come up with anything working (plus it would be a breaking change then).

Closes #174065

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation labels Dec 23, 2022
@ofborg ofborg bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Dec 23, 2022
@RaitoBezarius
Copy link
Member

Could you provide with the failed NixOS test so we can maybe help to fix it together?

@Patryk27
Copy link
Member Author

Could you provide with the failed NixOS test so we can maybe help to fix it together?

I've gotten stuck with the basic idea of the test, since it looks like I can't just run nixos-rebuild switch there (because VM's /nix/store is read-only + it doesn't have any channels configured); I've taken a look at at nixos/tests/installer.nix and nixos/tests/os-prober.nix, but I've failed to understand the magic that makes them work 😅

So I guess something very basic could be:

import ./make-test-python.nix {
  name = "nixos-rebuild";

  nodes =  {
    machine = { ... }: {
      # 
    };
  };

  testScript = ''
    machine.wait_for_unit("default.target")

    # TODO create /etc/configuration.nix (+ hardward-configuration.nix?) with
    #      multiple specializations; make following work:
    machine.succeed("nixos-rebuild switch --specialisation-name foo")
  '';
}

@RaitoBezarius
Copy link
Member

Could you provide with the failed NixOS test so we can maybe help to fix it together?

I've gotten stuck with the basic idea of the test, since it looks like I can't just run nixos-rebuild switch there (because VM's /nix/store is read-only + it doesn't have any channels configured); I've taken a look at at nixos/tests/installer.nix and nixos/tests/os-prober.nix, but I've failed to understand the magic that makes them work sweat_smile

So I guess something very basic could be:

import ./make-test-python.nix {
  name = "nixos-rebuild";

  nodes =  {
    machine = { ... }: {
      # 
    };
  };

  testScript = ''
    machine.wait_for_unit("default.target")

    # TODO create /etc/configuration.nix (+ hardward-configuration.nix?) with
    #      multiple specializations; make following work:
    machine.succeed("nixos-rebuild switch --specialisation-name foo")
  '';
}

If I provide you with a working baseline for the test, can you write the Python script? You can check systemd-boot.nix test which is interesting for your usecases.

@Patryk27
Copy link
Member Author

I know how those tests work in general (and I've written a few before), I'm just having a problem with this particular setup - so if you manage to provide some foundation, I should be able to work on that 🙂

@mweinelt mweinelt removed their request for review December 24, 2022 21:34
@ncfavier
Copy link
Member

but I've failed to understand the magic that makes them work

I think the magic you're looking for is installation-device.nix? That should at least take care of channels.

../modules/profiles/installation-device.nix
../modules/profiles/base.nix

@Patryk27
Copy link
Member Author

Okie, thanks, I'll look into it 🙂

@Patryk27
Copy link
Member Author

Okie, I've changed --specialisation-name into just --specialisation and added a test 🎉

The test still needs a minor clean-up (I've just copy-pasted system.extraDependencies from os-prober.nix and I feel like they could get shortened), but I think it's mostly ready.

@Patryk27 Patryk27 changed the title nixos: add --specialisation-name to nixos-rebuild nixos: add --specialisation to nixos-rebuild Dec 25, 2022
@ofborg ofborg bot requested a review from Profpatsch December 25, 2022 12:38
@Patryk27
Copy link
Member Author

kk, test minimized 🙂

@RaitoBezarius
Copy link
Member

@ofborg test nixos-rebuild-specialisations

@RaitoBezarius
Copy link
Member

The aarch64 failure seems unrelated to this PR.

Copy link
Member

@RaitoBezarius RaitoBezarius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

@RaitoBezarius
Copy link
Member

@Patryk27 I think this PR is good to go, there's some style changes to be done on it (trailing whitespace), can I let you take care of them?

This commit fixes a papercut in nixos-rebuild where people wanting to
switch to a specialisation (or test one) were forced to manually figure
out the specialisation's path and run its activation script - since now,
there's a dedicated option to do just that.

This is a backwards-compatible change which doesn't affect the existing
behavior, which - to be fair - might still be considered sus by some
people, the painful scenario here being:

- you boot into specialisation `foo`,
- you run `nixos-rebuild switch`,
- whoops, you're no longer at specialisation `foo`, but you're rather
  brought back to the base system.

(it's especially painful for cases where specialisation is used to load
extra drivers, e.g. Nvidia, since then launching `nixos-rebuild switch`,
while forgetting that you're inside a specialisation, can cause some
parts of your system to get accidentally unloaded.)

I've tried to mitigate that by improving specialisations so that they
create a dedicated file somewhere in `/run/current-system` containing
the specialisation's name (which `nixos-rebuild` could then use as the
default value for `--specialisation`), but I haven't been able to come
up with anything working (plus it would be a breaking change then).

Closes NixOS#174065
@Patryk27
Copy link
Member Author

Oh my, fixed! 😇

@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. and removed 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. labels Jan 15, 2023
@RaitoBezarius RaitoBezarius merged commit e2ac17f into NixOS:master Jan 15, 2023
@Patryk27 Patryk27 deleted the fix/174065 branch January 15, 2023 17:57
@donovanglover
Copy link
Member

Would it be possible to make --specialisation work for build-vm as well? I assume some logic needs to be added to nixBuild/nixFlakeBuild?

@Patryk27
Copy link
Member Author

On the top of my head, I think it should be possible; I myself have no time to prepare such change now, though 😢

@nh2
Copy link
Contributor

nh2 commented May 31, 2025

When passing a specialisation that doesn't exist, there is a lack of error message:

Patryk27 added a commit to Patryk27/nixpkgs-committers that referenced this pull request Dec 28, 2025
Hi,

I'd like to nominate myself - I've been submitting pull requests since 2020, started by packaging [netris](NixOS/nixpkgs#85382) and then went on to improving [LXD](NixOS/nixpkgs#89540), [packaging](NixOS/nixpkgs#91182) and [maintaining](https://github.com/NixOS/nixpkgs/pulls?q=author%3APatryk27+pcloud) pCloud (which, amazingly enough, required fixing a bug in [patchelf](NixOS/patchelf#544) itself!), and overall I've been implementing various quality of live improvements [here](NixOS/nixpkgs#207466) and [there](NixOS/nixpkgs#354755).

I've given [a talk about my Nix-driven blog](https://www.youtube.com/watch?v=_7wqXN-7ebw&t=6695s) during NixCon 2024 in Berlin, so it's possible we've already met irl!

Outside of Nix, I'm the maintainer of Rust's [AVR backend](rust-lang/rust#131651), with commits and merging rights to [LLVM](https://github.com/llvm/llvm-project/pulls?q=author%3APatryk27) - just adding as an extra "in case it helps" mention 😇 

Couple of weeks ago I decided to shift my priorities from working on custom projects to helping the communities I'm a part of more - having committer access to nixpkgs would allow me to make a greater impact as I intend to focus on reviewing and merging both ongoing and stale pull requests, since it seems there's always not enough people willing to review stuff!

Thanks, Patryk :-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: add --specialization option to nixos-rebuild

5 participants