NixOS

字数 733 · 2023-01-01

GC

1
nix-collect-garbage
1
2
3
4
nix-env --list-generations

sudo nix-collect-grabage --delete-older-than 14d
sudo nixos-rebuild switch

config

/etc/nixos/configuration.nix

nixos-rebuild switch

Home Manager

Module

Standalone

Firewall

1
networking.firewall.enable = false;

https://nixos.wiki/wiki/Firewall

Bluetooth

https://nixos.wiki/wiki/Bluetooth

1
2
3
4
5
6
7
8
bluetoothctl
# [bluetooth] # power on
# [bluetooth] # agent on
# [bluetooth] # default-agent
# [bluetooth] # scan on
# ...put device in pairing mode and wait [hex-address] to appear here...
# [bluetooth] # pair [hex-address]
# [bluetooth] # connect [hex-address]

upgrade

1
2
sudo nix-channel --update
sudo nixos-rebuild switch
1
2
# update flake lock file
nix flake update

env

1
2
3
4
5
# install package
nix-env -i python3

# remove package
nix-env -e python3

Nix Language

1
nix repl

Function

1
2
3
4
5
6
# define
mul = { a, b }: a * b

# call
mul { a = 3; b = 4; }
#> 12

Modules

Structure

1
2
3
4
5
6
7
8
9
{
  imports = [
    # paths to other modules
  ];

  options = {  };

  config = {  };
}

shorthand

1
2
3
4
5
6
7
8
{
  imports = [
    # paths to other modules
  ];

  # option definitions
  # ...
}

Eval

1
2
3
4
5
6
7
8
9
10
11
:l <nixpkgs.lib>

m1 = { options.foo = mkOption {}; }

m2 = { foo = "hello" }

m3 = { foo = "world" }

(evalModules { modules = [ m1 m2 m3 ]; }).config.foo

#> ["world" "hello"]

nixpkgs

https://github.com/NixOS/nixpkgs

Packaging

Phases

  • unpack
  • patch
  • configure
  • build
  • check
  • install
  • fixup

Ref

https://nixos.wiki
https://nixos.org/manual/nix
https://search.nixos.org/
https://nixos.org/guides/nix-pills/
https://nixos.org/manual/nixpkgs/stable

https://lantian.pub/en/article/modify-computer/nixos-packaging.lantian/

Glossary

NixOS

  • derivation - a build task

Others

  • GTK - A free and open-source cross-platform widget tookit for creating graphical user interfaces.
    • GIPM
    • Transmission
  • XDG - Cross-Destop Group

Tips

1
2
3
4
5
# run with spec packages
nix-shell -p python39 --run zsh

# or
nix shell nixpkgs#python39
1
2
3
4
5
6
7
8
9
# two channel in one system
let
  stable = import <nixpkgs> {};
  unstable = import <unstable> {};
in
mkShell {
  name = "amazing-shell";
  buildInputs = [ stable.gcc unstable.vscode ];
}