Skip to main content

Nix*

Nix can mean a few different things. I prefer to use Nix as a name for the Ecosystem and use more descriptive names for the particular components. No more confusion about Nix the language vs Nix the package manager.

  • Nixlang: the Nix language
  • Nixpac: the Nix package manager
  • Nixpkgs: the Nix packages collection
  • NixOS: the Nix Operative System

Note that the names Nixlang and Nixpac are not widely recognized. Use them at your own peril.

Advice on using the Nix package manager (Nixpac)#

  • Use flakes
  • Use home manager
  • Never use nix-env
  • Never use nix channels
  • Use direnv to automatically source your developer environments when you cd into them. Works beautifully in conjunction with nix.flake or default.nix
    • echo "use nix" >> .envrc && direnv allow (non-flakes)
    • echo "use flake" >> .envrc && direnv allow (flakes)

Developer environments#

This site (non-flake)#

This site is built with docusaurus. Having previously used jekyll and react-static, the super simple setup of docusaurus baffled me. I was up and running, writing content with hot reloads and netlify-compatible builds in 5 minutes.

default.nix looks like this:

with import <nixpkgs> {};mkShell {  buildInputs = with pkgs; [    nodejs    yarn  ];}$

Install & run:

cd skogsbrusnpx @docusaurus/init@latest init skogsbrus classiccd skogsbrusyarn start

An equivalent solution can be made with flakes as well - and that would actually be preferable as well. With flakes you can pin the exact version of all software dependencies, whereas this example with default.nix just takes whatever version of nodejs and yarn is the default in Nixpkgs.

Terraform (flake)#

This is a flake.nix I use when working with Terraform.

{  inputs = {    nixpkgs.url = "github:nixos/nixpkgs";    flake-utils.url = "github:numtide/flake-utils";  };
  outputs = { self, nixpkgs, flake-utils }:    flake-utils.lib.eachDefaultSystem (system:      let pkgs = nixpkgs.legacyPackages.${system};      in      {        devShell = with pkgs; pkgs.mkShell {          buildInputs = [            terraform_1            python38            pre-commit          ];        };      }    );}