Basic User Commands

System Updates

# Update entire system
nixos-rebuild switch

# Check what updates are available
nix-env -qa --available

# Install specific package
nix-env -iA nixos.firefox

# Remove package
nix-env -e firefox

The nixos-rebuild switch command is your primary tool for system updates. Think of it as pressing a big “update everything” button. It reads your system configuration and makes all necessary changes. The switch part means it will immediately activate the new configuration.

Basic Maintenance

# Clean up old packages
nix-collect-garbage

# List installed packages
nix-env -q

# Check system status
systemctl status

# View system logs
journalctl -xe

Regular maintenance keeps your system running smoothly. The garbage collector removes unused packages and old system configurations, freeing up disk space. Always check system logs if something isn’t working as expected.

Intermediate Commands

Configuration Management

# Edit system config
sudo nano /etc/nixos/configuration.nix

# Test configuration
nixos-rebuild test

# Build without activating
nixos-rebuild build

# Switch to new config
nixos-rebuild switch

Configuration management is where NixOS really shines. Your entire system configuration lives in files that you can version control. The test command lets you try new configurations without committing to them - perfect for experimenting.

Profile Management

# List generations
nix-env --list-generations

# Roll back to previous
nixos-rebuild switch --rollback

# Delete old generations
nix-env --delete-generations old

# Switch to specific generation
nix-env --switch-generation 123

Generations are snapshots of your system configuration. They’re your safety net - if something breaks, you can always roll back to a working state. Think of them as save points in a game.

Advanced Operations

Store Management

# Query dependencies
nix-store -q --references path

# Find referrers
nix-store -q --referrers path

# Optimize store
nix-store --optimize

# Verify integrity
nix-store --verify --check-contents

The Nix store is where all packages and configurations live. Each package has a unique hash-based path, ensuring reproducibility. Store operations let you inspect and maintain this complex system.

Development Environment

# Enter dev shell
nix-shell

# Shell with packages
nix-shell -p pkg1 pkg2

# Build development project
nix-build

# Create development environment
nix develop

Development environments in NixOS are completely isolated. You can have different versions of the same package for different projects without conflicts. The nix-shell command creates temporary environments with exactly the packages you need.

Expert Techniques

Channel Management

# Add custom channel
nix-channel --add url name

# Update specific channel
nix-channel --update name

# Remove channel
nix-channel --remove name

# List channels
nix-channel --list

Channels are like package repositories in other systems, but more powerful. They can contain not just packages, but entire system configurations. Expert users often maintain their own channels for custom packages.

Remote Operations

# Copy to remote
nix copy --to ssh://machine pkg

# Build on remote
nix build --remote-build

# Distribute builds
nix-copy-closure --to user@machine

# Deploy configuration
nixos-rebuild switch --target-host

NixOS excels at distributed operations. You can build on remote machines, copy packages between systems, and deploy configurations across your infrastructure. The hash-based store ensures everything stays consistent.

Best Practices

System Management

# Regular maintenance
nix-collect-garbage -d
nix-store --optimize
nixos-rebuild switch --upgrade

# Emergency recovery
nixos-rebuild switch --rollback
nix-env --rollback

Always keep at least one known-good generation. Test major changes in a VM first. Use version control for your configurations. Regular garbage collection keeps your system lean.

Performance Optimization

# Cache management
nix-store --optimize
nix copy --to local
nix-store --verify

# Build optimization
nix-build --cores 0
nix-build --max-jobs auto

Optimize your Nix store regularly. Use binary caches when possible. Distribute builds across cores. Monitor your disk usage - the Nix store can grow large over time.

Troubleshooting

Common Issues

# Fix broken dependencies
nix-store --verify --check-contents
nix-store --repair

# Clear package cache
nix-channel --update
nix-env -iA nixos.nix

# Debug builds
nix-build -K -v

Most issues stem from broken dependencies or corrupt store paths. The verify and repair commands can fix many problems. Always check the Nix log files for detailed error messages.

Advanced Debugging

# Trace evaluation
nix-instantiate --trace-verbose

# Profile builds
nix-build --trace-function-calls

# Analyze closure
nix-store -q --graph

# Check references
nix-store -q --tree

Understanding build traces and closure graphs is crucial for debugging complex issues. Use these tools to understand exactly what Nix is doing during builds and evaluations.

0 Comments for this cheatsheet. Write yours!