Nix Snippets
nix-env - system wide programs
Section titled “nix-env - system wide programs”# View packages and search for firefox# or search at Nix https://search.nixos.org/packagesnix-env -qaP firefox
# Install package like Firefoxnix-env -iA nixpkgs.firefox
# List installed packagesnix-env -q
# Update all nix packagesnix-env -u
# Install a packagenix-env -iA nixpkgs.<package>
# Rollback packages to a previous generation (last set of upgrades)nix-env --rollback
# Uninstall package# Note name is a regular name and not with nixpkgs prefixnix-env -e firefox# ornix-env --uninstall firefox
# Uninstall all nix-env packages, Warning, removes itself as wellnix-env --uninstall '*'
nix-channel, nix-collect-garbage, nixos-rebuild - updates and clean
Section titled “nix-channel, nix-collect-garbage, nixos-rebuild - updates and clean”# Update Nix indexnix-channel --update
# List current channelnix-channel --list
# Add the current stable channel as of 2024-08nix-channel --add https://channels.nixos.org/nixos-24.05 nixpkgs# Add unstable channelnix-channel --add https://nixos.org/channels/nixpkgs-unstable
# Remove a channel using its alias, in this case nixpkgs## Common fix for "Warning: name collision in input Nix expressions, skipping"## Is to remove the channel causing the collision for a user## since the channel is duplicated in the root## as root, check nix-channel --listnix-channel remove nixpkgs
# NixOS Rebuild from updated configurationssudo nixos-rebuild switch
# Clean up old generations and packages to prevent space issuesnix-collect-garbage
# Clean up generations older than 30 daysnix-collect-garbage --delete-older-than 30d
# NixOS garbage collectionsudo nix-collect-garbage --delete-older-than-30d
# Active repl for commands, :q or Ctrl + d to exitnix repl
## Flakes
### Updatenix flake update
nix-shell
Section titled “nix-shell”Reproducible shell environments
source: https://nix.dev/tutorials/first-steps/ad-hoc-shell-environments.html
- See also: Automatic environment activation with direnv — nix.dev documentation and Setting up a Python development environment — nix.dev documentation
# Activate shell with several programsnix-shell -p git neovim nodejs
# Specific version of programs## --run execute in bash, the command## --pure discard most environment variables set on your system## -I source of package declarationsnix-shell -p git --run "git --version" --pure -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/2a601aafdc5605a5133a2ca506a34a3a73377247.tar.gz
shell.nix
Section titled “shell.nix”Example at: https://github.com/rust-lang/rustlings
# Run nix-shell with shell.nix file below# to get a reproducible environmentnix-shell
- Install software, set environment variables, and run commands before
shell with
shellHook
with everything inmkShell
shell.nix
sets up a rust environment
let nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11"; pkgs = import nixpkgs { config = {}; overlays = []; };in
pkgs.mkShell { packages = with pkgs; [ cargo rustc rust-analyzer rustfmt clippy vim ];
GIT_EDITOR = "${pkgs.neovim}/bin/vim";
shellHook = '' git status '';}
-
To pin a version of nixpkgs, use
fetchTarball
to get a specific version of nixpkgs:{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/06278c77b5d162e62df170fec307e83f1812d94b.tar.gz") {}}:...
Home Manager
Section titled “Home Manager”# Update home manager and create back up files if overwritting fileshome-manager switch -b backup