% NixOS First steps % サニエ エリック % 2016年12月07日 - Tokyo NixOS Meetup
- Understanding NixOS structure
- Get familiar with NixOS
- Configuration File
- Channels
- Maintenance
- Uses Nix as a package manager
- Source and binary packages
- A single configuration file controls the OS
- System versioning and rollbacks
- Multi-user package management
- Separation of environments and filesystem (via profiles)
configuration.nix
:- Configuration file that manage every aspect of the OS
- Nix Store:
- Where all Nix products are stored
- nixpkgs:
- Collection of Nix packages "recipes" and NixOS modules definitions
- Channels:
- Way to synchronize nixpkgs
- Profiles:
- Way to manage environments, per user, revisioned
- Located
/etc/nixos/configuration.nix
- written in Nix Expression Language
- functional language, close to untyped lambda calculus
- can be splitted in multiple files via
imports
- NixOS modules
- modules define options
- options are used in
configuration.nix
- Thousands of options
- can even configure vim
- Provisioning with
environment.systemPackages
- services, programs, setting files, boot, kernel modules, users, ...
- Possible to import custom modules with
imports
man configuration.nix
,nixos-option
,nixos-help
commands- Links:
-
Overriding packages dependencies:
environment.systemPackages = with pkgs; [ (nginx.override { openssl = libressl; }) ];
-
Using custom packages:
environment.systemPackages = with pkgs; [ (import ./my-pkg.nix) (pkgs.callPackage ./my-other-pkg.nix) ];
nixos-rebuild
commandnixos-rebuild switch
: rebuild, activate the configuration and add it to the boot menu.nixos-rebuild test
: rebuild, activate the configuration but don't add it to the boot menu.nixos-rebuild build-vm
: create a QEMU virtual machine with the configuration.-I nixos-config=/etc/nixos/custom-configuration.nix
to use a different configuration file.-I nixpkgs=/path/to/nixpkgs
to use a custom version of nixpkgs.
-
Location
/nix/store/
-
Contains:
- Every builded package
- Every channel version
- Every profile
-
Every store item is compartimented in its own folder
-
Example:
$ nix-build -A hello '<nixpkgs>' $ ls result -dl $ tree result
- collection of
- Nix packages "recipes"
- NixOS modules
- NixOS tests
- and more
- Links:
- External point of view: Channels are a way to distribute specific versions of nixpkgs
- Internal point of view: Channels are a way to sync nixpkgs that meet some requirements
- Version of nixpkgs to use, multiple variants:
*-XX.XX
: stable channels, new one every 6 months*-unstable
: unstable channels, close to rolling release*-small
: small channels, good for servers
-
nix-channel
command to manipulate channelsnix-channel --list
list used channelsnix-channel --add
to add a channelnix-channel --update
to update the channels
-
Example: using the unstable channel:
$ nix-channel --add https://nixos.org/channels/nixos-unstable nixos $ nix-channel --update
-
Channels information is stored in
~/.nix-channels
-
Active channels are linked in
~/.nix-defexpr/
-
All channels and their versions are stored under
/nix/var/nix/profiles/per-user
-
root
user channel is the system channel (used bynixos-rebuild
)
-
A remote nixpkgs archive can be used:
$ nixos-rebuild switch -I nixpkgs=https://github.com/nixos/nixpkgs-channels/archive/nixos-16.09.tar.gz
-
A local checkout of nixpkgs, useful for custom patches:
$ nixos-rebuild switch -I nixpkgs=/path/to/my/nixpkgs
-
User channels version can be checked with:
$ nix-env -p /nix/var/nix/profiles/per-user/USER/channels --list-generations
-
And their version changed with:
$ nix-env -p /nix/var/nix/profiles/per-user/USER/channels --switch-generations GEN
- Profiles merge nix-store paths by symbolic links
- Active profile is in
~/.nix-profile
- Profiles are managed and versioned in
/nix/var/nix/profiles/per-user
- Profiles are managed and versioned in
- configuration.nix
environment.systemPackages
profile is linked to/run/current-system/sw
- Links:
-
nix-shell
command to generate temporary environments:$ nix-shell -p hello $ hello
-
can even run commands:
$ nix-shell -p cowsay --run "cowsay hello nix"
-
nix-shells can be declared in nix-files, handy for setting dev environments
-
nix-shell can also be used as shebang
-
nix-env is complex, nox is more user-friendly:
$ nix-shell -p nox $ nox browser
-
Watch for the store size!
-
Nix store all the packages in the nix store (
/nix/store
) -
Removing packages with
nix-env -e
or by removing them fromenvironment.systemPackages
dont remove them from the store -
Store can be cleaned by garbage-collecting it:
# nix-collect-garbage --delete-older-than 14d