Nix Commands
Common Nixos Commands
Some of the commands that i use and want to keep a record of.
Building Nixos
sudo nixos-rebuild switch
Below is for using flakes.
sudo nixos-rebuild switch --flakes ./#desktop
"." should be the directory of the flake you want to build and #desktop should denote what you are calling the system in your flake.nix. More info here: Flakes
You might need to rollback to the previous config, using this command.
sudo nixos-rebuild switch --rollback
Searching for Packages
When you want to install a program you need to know its name to put in either your configuration.nix or to specify in your nix-shell command.
nix search nixpkgs yazi
This will show you all packages in the repository that have "yazi" in the name, the more specific you can be the better output you will get.
Currently no way to search your package overlays this way. You can go here Nix Search to search the current stable version and the unstable packages for the name you need.
Nix-shell commands
Sometimes you need a program for one task but dont want to install and remove it, nix-shell can do this.
sudo nix-shell -p lf
This will install 'lf' and crreate a terminal you can use it in, exit to leave that terminal and no longer have the program installed.
You can install multiple programs in one shell:
sudo nix-shell -p lf tmux gzip
Cleaning up Nix-store/Nixos
After generating new configs and temporarily installing shell programs, you will want to cleanup some space.
Nixos Generations
This will clean the Nix Store and the Nixos generations.
sudo nix-collect-garbage -d --dry-run
Will show you the genereations that are going to be removed.
sudo nix-collect-garbage -d --delete-older-than 14d
Would remove Nixos generations older than 14days.
To make your current NixOs generation the default boot generation run the following.
/run/current-system/bin/switch-to-configuration boot
Nix-store
sudo nix-store --gc --print-dead
Will show you the packages that will be removed from the nix store.
sudo nix-store -gc
To remove everything that is no longer being referenced.
NixOs Channels
Use a FLAKE!!!! The channels are not being used often
This is the distribution of NixOs and the channel that you are using determines the version of NixOS that you are using. They are catogorized as stable and unstable, large and small. https://nixos.wiki/wiki/Overview_of_the_NixOS_Linux_distributionListing the current Channels.
nix-channel --list
Adding a primary channel.
nix-channel --add https://nixos.org/channels/channel-name nixos
Removing a Channel.
nix-channel --remove channel-alias
Replace channel-alias with the alias for the channel you want to remove.
Updating all channels or specifying a channel.
nix-channel --update channel-alias
Lastly you can rollback a channel update if you have issues with an update.
nix-channel --rollback
As always you will need to rebuild your NixOs after this.