Skip to content
Andrew Parker edited this page Dec 21, 2019 · 14 revisions

Manual is here: https://nixos.org/nixos/manual/index.html

  • Declarative:nixos-rebuild will ensure that you get a consistent set of binaries corresponding to configuration.nix. Must be a root user to change.
  • Ad hoc goes through nix-env command, allows mixing packages from different Nixpkgs versions. This is the only way non-root users can add packages.

Search for an application containing the name "thunderbird".

$ nix-env -qaP 'thunderbird' --description
nixos.thunderbird  thunderbird-60.5.1  A full-featured e-mail client

Enable an application in configuration.nix

environment.systemPackages = [ pkgs.thunderbird ];

Rebuild

$ nixos-rebuild switch

If you have a*.nix file, you can do this:

Add a *.nix file to configuration.nix

environment.systemPackages = [ (import ./my-hello.nix) ];

Test it:

$ nix-build my-hello.nix
$ ./result/bin/hello
Hello, world!

Install by the attribute name, e.g.: nixos.thunderbird

$ nix-env -Ai nixos.thunderbird

Upgrade a package, e.g.: nixos.thunderbird

$ nix-channel --update nixos  # First update the channel.
$ nix-env -Ai nixos.thunderbird  # Update just this package.

Upgrade all packages for which there is a newer version:

$ nix-env -u '*'

Uninstall a package:

$ nix-env -e thunderbird

Rollback an undesirable nix-env action:

$ nix-env --rollback

The driver is under an "unfree" license, so you have to add the following to configuration.nix to allow it:

  { nixpkgs.config.allowUnfree = true; }

Enable like so and then reboot:

services.xserver.videoDrivers = [ "nvidia" ];
hardware.opengl.driSupport32Bit = true;   # Enables support for 32-bit programs such as Wine.

Periodically, you need to run the garbage collector to remove old packages:

$ nix-collect-garbage

Occasionally, you wan to t remove old system configurations (you lose the ability to roll back):

$ nix-collect-garbage -d

After the system has booted, you can make the selected configuration the default for subsequent boots:

$ /run/current-system/bin/switch-to-configuration boot

Switch to previous system configuration:

 nixos-rebuild switch --rollback

Switch to an arbitrary system configuration:

$ /nix/var/nix/profiles/system-N-link/bin/switch-to-configuration switch

where N is the number of the NixOS system configuration.

To get a list of the available configurations, do:

$ ls -l /nix/var/nix/profiles/system-*-link
...
lrwxrwxrwx 1 root root 78 Aug 12 13:54 /nix/var/nix/profiles/system-268-link -> /nix/store/202b...-nixos-13.07pre4932_5a676e4-4be1055

If the corruption is in a path in the closure of the NixOS system configuration, you can fix it by doing:

$ nixos-rebuild switch --repair

You can also scan the entire Nix store for corrupt paths:

nix-store --verify --check-contents --repair

Any corrupt paths will be redownloaded if they’re available in a binary cache; otherwise, they cannot be repaired.

Exploring NixOS Environment

nixos-option command

With multiple modules, it may not be obvious what the final value of a configuration option is. The command nixos-option allows you to find out:

$ nixos-option services.xserver.enable
true

$ nixos-option boot.kernelModules
[ "tun" "ipv6" "loop" ... ]

nix repl command

nix repl brings up a read-eval-print loop to interactively explore the NixOS environment.

$ nix repl '<nixpkgs/nixos>'

nix-repl> config.networking.hostName
"mandark"

nix-repl> map (x: x.hostName) config.services.httpd.virtualHosts
[ "example.org" "example.gov" ]