CuSO4_Deposit's Electrolytic Infodump

Nix Language Basics

Nix is Functional, Lazy evaluated.

interactive interpreter: nix repl. Execute a file containing nix expression: nix-instantiate --eval file.nix.

let expression (let binding): specify binding scope

datatypes:

with expression: access attributes without repeatedly referencing the attribute set

inherit expression: shorthand for assigning value from existing scope to the same name in a nested scope

string interpolation: ${...}

paths

Functions

x: x + 1
x: y: x + y
{ a, b }: a + b
{ a, b ? 0 }: a + b
{ a, b, ...}: a + b
args@{ a, b, ... }: a + b + args.c

or

{ a, b, ... }@args: a + b + args.c

Calling Functions

let f = x : x + 1; in f 1

or

let f = x: x.a; in f { a = 1; }
(x: x + 1) 1

Function Libraries

a = "Hell${builtins.toString 0} World!";

or

a = "Hell${toString 0} World!";
+ import

  + takes a path to a Nix file, reads it and evaluate the contained nix expression, and returns the result value.
let
  pkgs = import <nixpkgs> {};
in
pkgs.lib.strings.toUpper "lookup paths considered harmful"

Fetchers

builtins.fetchurl "https://github.com/NixOS/nix/archive/7c3ab5751568a0bc63430b33a5169c5e4784a0f

Derivations

Worked Examples

{ pkgs ? import <nixpkgs> {} }:
let
  message = "hello world";
in
pkgs.mkShellNoCC {
  buildInputs = with pkgs; [ cowsay ];
  shellHook = ''
    cowsay ${message}
  '';
}
{ config, pkgs, ... }: {
  imports = [ ./hardware-configuration.nix ];
  environment.systemPackages = with pkgs; [ git ];
  # ...
}

#Nix