sys/default.nix

本文件存放喆我的NixOS的配置。 NixOS的配置通过命令sudo nixos-rebuild switch生效。 因为NixOS的配置入口是/etc/nixos/configuration.nix, 所以需要在/etc/nixos/configuration.nix中import本文件,例如

# /etc/nixos/configuration.nix:
{ config, pkgs, ... }: {
  imports = [
    ./hardware-configuration.nix
    # import my sys/ here!
    /home/xieby1/.config/nixpkgs/sys
  ];
  # other configs ...
}

NixOS配置可以使用的参数可参考configuration.nix的manpage,即man configuration.nix。 下面是我的NixOS的配置源码及注解:

# add this file to /etc/nixos/configuration.nix: imports
{ config, pkgs, ... }:

{

让NixOS的nixpkgs使用和home-manager的nixpkgs采用相同的nixpkgs config

  nixpkgs.config = import ../config.nix;

导入我的NixOS的CLI和GUI配置, 详细内容见文档./cli./gui

  imports = [
    ./modules
    ./cli
    ./gui
  ];

  nix.settings = {
    substituters = [
      # Tuna often throws 502, so replace it with USTC
      "https://mirrors.ustc.edu.cn/nix-channels/store"
      "https://cache.nixos.org/"
      "https://xieby1.cachix.org"
    ];
    trusted-public-keys = [
      "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
      "xieby1.cachix.org-1:hVhQjWqdV3oGsSnyXSvzPgmDqcKm+EeKFtqsNK+eRew="
    ];
    experimental-features = ["nix-command" "flakes"];
    # make builtins.fetchurl expire in a long time
    # although `man nix.conf` says tarball-ttl default is 4294967295
    # `nix show-config` show default is 3600 (1 hour)
    tarball-ttl = 4294967295; # max 32bit integer
  };

  nix.channel.enable = false;
  # Preserve NIX_PATH for root and wheel
  # https://superuser.com/questions/232231/how-do-i-make-sudo-preserve-my-environment-variables
  security.sudo.extraConfig = ''
    Defaults:root,%wheel env_keep+=NIX_PATH
  '';

设置时区。

  time.timeZone = "Asia/Shanghai";

设置Linux账户信息。

  users.mutableUsers = false;

当然GitHub上当然不能明文存储密码,这里使用hash过的密码。 可以使用命令行工具mkpasswd来生成hash过的密码。 给root用户设置hash过的密码。

  users.users.root.hashedPassword = "$6$wRBpbr4zSTA/nh$XI/KUASw3mELIqyAxN1hUTWizz9ZBzPhP2u4HNDCA49h4KOWkZsyuiextyXkUti7jYsUHE9fTiRjGAoxBg0Gq/";

将 zsh 设为登录 shell。 此前使用 bash-zsh-forward trick(在 ~/.bashrc 中 exec zsh),但遇到两个问题:

  1. 显示管理器会话包装器(如 niri-session)会通过 bash -l 重新执行, 触发 ~/.bashrc 中的 zsh forward,导致桌面环境被劫持而无法启动。
  2. bash -i 命令(如 kitty bash -i fzf-doc)会被立即转发到 zsh, 导致目标命令永远无法执行。 因此直接在 NixOS 系统配置中设置 zsh 为登录 shell。 同时需要 programs.zsh.enable = true 确保 zsh 被安装并出现在 /etc/shells 中。
  programs.zsh.enable = true;
  users.users.xieby1 = {
    shell = pkgs.zsh;
    isNormalUser = true;
    createHome = true;

同上,给xieby1用户设置hash过的密码。

    hashedPassword = "$6$Y4KJxhdaJTT$RSolbCpaUKK2UW1cdnuH.8n1Ky9p0Lnx0MP36BxGX9Q2AeVMjCp.bZOsZ11w689je/785TFRQoVgicMiOfA9B.";

给用户xieby1启用sudo。

    extraGroups = [ "wheel" ];

ssh授权的公钥。这样设置后,我的所有的NixOS都相当于“自动授权”了。 我的/home/xieby1/Gist/文件夹存放着一些不方便放入Git仓库的文件,比如二进制文件,或是隐私文件。 该文件由syncthing进行多设备同步。 简单的说,我的备份理念是“git备份配置,syncthing备份数据”。 “配置”即指这个nix_config仓库,“数据”指~/Gist/~/Docs/等文件夹。 有了这些备份就能轻松还原/复现我的整个工作环境。 TODO:单独专门介绍我的备份理念。

    openssh.authorizedKeys.keyFiles = [] ++ (
      if builtins.pathExists /home/xieby1/Gist/Vault/y50_70.pub
      then [/home/xieby1/Gist/Vault/y50_70.pub]
      else []
    ) ++ (
      if builtins.pathExists /home/xieby1/Gist/Vault/yoga14s.pub
      then [/home/xieby1/Gist/Vault/yoga14s.pub]
      else []
    );
  };

让TTY自动登录我的账户,这样就可以自动启动用户级(user)的systemd服务了。 这样就可以在非NixOS中(比如Ubuntu服务器、WSL2、Debian树莓派等) 自动拉起systemd用户服务(比如syncthing、clash、tailscale等)。

  services.getty = {
    autologinUser = "xieby1";
    autologinOnce = true;
  };

有关systemd用户服务的配置,详细可见参考:

  services.logind.settings.Login = {
    # On battery: closing the lid suspends.
    HandleLidSwitch = "suspend";
    # On AC power: keep running when lid closes.
    HandleLidSwitchExternalPower = "ignore";
    # With external monitor/dock: keep running.
    HandleLidSwitchDocked = "ignore";
  };
}