使用 NixOS 的個人旅程

January 31, 2023

前言

早晨!我使用 NixOS 己經有一段日子,所以決定整理一篇文章來記錄學到的知識。

管理套件

一般來說,使用 nix-env 或編輯 /etc/nixos/configuration.nix 進行安裝,刪除及更新套件, 而後者會產生一個新的 Generations

  • 列舉己經安裝的套件
    $ nix-env --query --installed 
  • 安裝套件
    $ nix-env -iA nixos.<package>
    $ nix-env -f channel:nixpkgs-unstable -iA <package>
  • 刪除套件
    $ nix-env -e <package>
  • 更新套件
    $ sudo nix-channel --update
    $ export NIXPKGS_ALLOW_UNFREE=1; nix-env -u '*'
  • GPG 服務作為例子,陳述如何修改 /etc/nixos/configuration.nix
    $ sudo vim /etc/nixos/configuration.nix
    注意︰以下只展示修改的部份。
    {
      # List packages installed in system profile.
      environment.systemPackages = with pkgs; [
        gnupg
        pinentry
      ];
    
      # Some programs need SUID wrappers, can be configured further or are
      # started in user sessions.
      programs.mtr.enable = true;
      programs.gnupg.agent = {
        enable = true;
        enableSSHSupport = true;
      };
    }
    $ sudo nixos-rebuild switch
    $ sudo reboot now
    重新開機後就能夠看到新的 Generations,選擇並再次登入系統,然後進行檢查。
    $ systemctl --user status gpg-agent
    gpg-agent.service - GnuPG cryptographic agent and passphrase cache
         Loaded: loaded (/etc/systemd/user/gpg-agent.service; linked-runtime; vendor preset: enabled)
        Drop-In: /nix/store/7j198b9dkcprl2w76aiyn4i7zl7f32fn-user-units/gpg-agent.service.d
                 └─overrides.conf
         Active: active (running)

Generations 的儲存空間

Generations 的儲存路徑在 /boot/efi/EFI/nixos,當系統不能夠產生新的 Generations,那就請檢查目前的儲存空間是否充足。

$ du -h /boot/efi/EFI/nixos
1.0K    /boot/efi/EFI/nixos/.extra-files
30M     /boot/efi/EFI/nixos
$ df -h /boot/efi/
Filesystem      Size  Used Avail Use% Mounted on
/dev/nvme0n1p1   96M   58M   39M  61% /boot/efi

使用 nix-collect-garbage 就能夠刪除指定時間內的 Generations,以下例子指定了 30 天。

$ sudo nix-collect-garbage --delete-older-than 30d
$ sudo nixos-rebuild switch

NVIDIA Driver on NixOS

由於 NVIDIA 的驅動程式是 unfree,所以必須在 /etc/nixos/configuration.nix 中進行聲明。

$ sudo vim /etc/nixos/configuration.nix

注意︰以下只展示修改的部份。

{
  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;
  
  # NVIDIA official drivers
  services.xserver.videoDrivers = [ "nvidia" ];
  hardware.opengl.enable = true;
  
  # Optionally, you may need to select the appropriate driver version for your specific GPU.
  hardware.nvidia.package = config.boot.kernelPackages.nvidiaPackages.stable; 
}
$ sudo nixos-rebuild switch
$ sudo reboot now

Alacritty

它的範本設定檔的預設路徑和其他 Linux 發行版不太一樣,所以就特別寫一下。

/nix/store 搜尋範本設定檔 alacritty.yml

$ find /nix/store -type f -name "alacritty.yml" 
/nix/store/a6izfm5dw02xycdq3q222d2qc28g4c77-alacritty-0.10.1/share/doc/alacritty.yml

複製至家目錄

$ cp /nix/store/a6izfm5dw02xycdq3q222d2qc28g4c77-alacritty-0.10.1/share/doc/alacritty.yml ~/.alacritty.yml

進行客制化,這個例子只有設定 working_directory

$ vim ~/.alacritty.yml
# Startup directory
#
# Directory the shell is started in. If this is unset, or `None`, the working
# directory of the parent process will be used.
working_directory: /home/gapry/Workspaces 

Toolchain

我們可以透過撰寫 *.nix 切換 GCCClang

  • GCC
    $ vim toolchain.nix
    with import <nixpkgs> {};
    stdenv.mkDerivation {
      name        = "GNU Toolchain";
      buildInputs = [
        llvmPackages_14.libcxxClang # clang-format
      ];
    }
    $ nix-shell toolchain.nix --command "gcc --version; clang-format --version"    
    gcc (GCC) 11.3.0
    
    clang-format version 14.0.1
  • Clang
    $ vim toolchain.nix
    with import <nixpkgs> {};
    
    (mkShell.override { stdenv = llvmPackages_14.stdenv; }) {
      name        = "LLVM toolchain";
      buildInputs = with pkgs; [
        gdb
      ];
    }
    $ nix-shell toolchain.nix --command "clang --version; gdb --version"
    clang version 14.0.1
    
    GNU gdb (GDB) 12.1

後記

本文整理了一些簡單實用的小技巧,希望可以吸引更多的人試用 NixOS。 如果撰寫 *.nix 的時候出現困難,可以嘗試利用 ChatGPT。 這一則 Tweet 是我之前的實驗記錄,僅供參考。 多謝!


Profile picture

Written by Gapry, 魏秋
Twitter | GitHub | Facebook | Mastodon