Need help in Updating nixos using flakes

Im using flakes with homemanager. and i was trying to update my os(sudo apt-get update upgrade).
At the time of initial installation, it was 23.05 and later to move from 23.05 to 23.11 by changing the os version numbers inside the flake.nix file.

when i listed out my channels, it was pointing at 23.05

❯ sudo nix-channel --list

so i thought i was making mistake and i removed it, then added 23.11 like this

 sudo nix-channel --remove nixos
[sudo] password for user: 
uninstalling 'nixos-23.05'
building '/nix/store/xsk9wjy0dpp7w7d3r3bkzxzaklqdzq6q-user-environment.drv'...
❯ sudo nix-channel --list            # No output
❯ sudo nix-channel --add
❯ sudo nix-channel --list

then, when i gave

❯ nix-channel --update
this derivation will be built:
building '/nix/store/p4sf0gc5wagmv7y35lypa91b5fjh04wc-home-manager-23.05.tar.gz.drv'...
unpacking channels...

it says it will build 23.05 still,
but, if i try to list my channels it shows im at 23.11

❯ sudo nix-channel --list

what am i doing wrong here ?

My actual plan of command list to update my system was,(correct me if im wrong please)

  1. sudo nix-channel --list (check if i was in the right channel)
  2. in my case, remove 23.05 and add 23.11
  3. nix-channel --update (Im not sure if i should have done this, correct me please)
  4. then, should i give nix flake lock --update-input nixpkgs or should i give nix flake update . ?
  5. and here comes my actual doubt, What command should i give exactly beyond this point to rebuild and switch (reason im asking this is, each time when i add a package either under home manager or sys-config file it breaks my os version and list in the boot menu)
  6. what commands should i follow to remove generations from boot list (because, im facing a weird issue which i have asked earlier and haven’t got any solutions yet )

I think there is a misunderstanding going on here. If you manage your system configuration with a flake then you don’t use channels.

Just change the inputs to 23.11 in your flake.nix:

inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";

and run

nix flake update

followed by a

sudo nixos-rebuild boot --flake .#configName

That won’t update things installed with nix-env, nix profile, or a standalone home-manager installation, by the way, so double check those and either migrate them to your flake or add a step for them to your update routine.

1 Like

Aside from flakes being independent of Channels, you should probably have called

sudo nix-channel --add nixos

to add the new channel under the same name as the one you removed.

1 Like

As far as I know, the boot menu is updated and entries are generated when doing nixos-rebuild. That means, just doing sudo nix-collect-garbage -d won’t remove old generations from your boot menu. (Furthermore, if you reboot and select one of those old generations the system will hang, forcing you to reboot again! But it is clear why this happens (and the system works as expected), because the generation you try to boot doesn’t exist anymore.) To get rid of the boot entries simply do nixos-rebuild switch again.

Now concerning the mixed-up generation numbers and build times you see in the screenshot and terminal output in your linked post:
Consider the following scenario: you change your system configuration by adding a package A and realize it by running nixos-rebuild switch (which creates a new generation, let’s say 14). Soon after, you find out that adding package A was a mistake and remove it from your config, followed by another nixos-rebuild switch. The new generation would now be 15. But Nix checks if the desired configuration already exists in the Store (by comparing the hashes/paths), finds that 15 is identical to 13, and instead of creating it just points to the already existing generation 13. That explains why a seemingly newer generation (i.e. 15) can have an older build time in the boot menu than a seemingly older generation (i.e. 14). You can check your available generations and compare their hashes with

ls -l /nix/var/nix/profiles/

Judging by the terminal output you provided in your linked post, the two generations in question were created in quick succession shortly after midnight. So it’s reasonable to assume that what I described in the hypothetical scenario above happened here too.

1 Like

So you mean to say, i don’t have to use --remove and just keep adding new version when they arrive, or its not necessary to do --add at all and instead just changing the version number inside the flakes.nix file itself is sufficient enough ?

In my current scenario, will it be advisable if i do

 sudo nix-channel --remove

and get back to original state by running

sudo nix-channel --add nixos

nix-channel commands will not effect your flake at all. To update your flake, you need to edit your flake.nix file and change the nixpkgs URL to the new version

1 Like

I was having a guess, but was not sure and couldn’t find the answer, thanks a lot for the clarification.

sorry, im unable to get it. Im am still new to nixos

That makes sense. I get it now :handshake: thanks a lot for looking into my earlier issue and taking your time to analyze things through the screenshot and explaining it in detail.

1 Like