AgentSkillsCN

fact-nix

Nix 领域知识——flake 结构、lib.codgician 函数、模块模式、主机引用

SKILL.md
--- frontmatter
name: fact-nix
description: Nix domain knowledge - flake structure, lib.codgician functions, module patterns, hosts reference

Nix Domain Knowledge

Repository Layout

code
serenitea-pot/
├── flake.nix         # Entry point
├── lib/              # lib.codgician namespace
├── hosts/            # Auto-discovered machine configs
│   ├── darwin/       # macOS: furina, raiden-ei
│   └── nixos/        # Linux: fischl, focalors, lumine, nahida, paimon, sandrone, wanderer, xianyun
├── modules/          # System-level modules
│   ├── generic/      # Cross-platform
│   ├── darwin/       # macOS-specific
│   └── nixos/        # Linux-specific (services here)
├── hm-modules/       # Home Manager modules
├── packages/         # Custom derivations + terraform-config/
├── overlays/         # Package overrides (00-* first, 99-* last)
├── secrets/          # Agenix .age files
├── apps/             # CLI: tfmgr, chkexp, mkimg, mkjwe
└── shells/           # Dev environments

Hosts

HostPlatformTypeNotes
furinaaarch64-darwinPhysical MacApple Silicon
raiden-eix86_64-darwinPhysical MacIntel
paimonx86_64-linuxBare metalPrimary server
fischlx86_64-linuxBare metalHypervisor
sandronex86_64-linuxBare metalCIX 8180
lumineaarch64-linuxCloud VMAzure
xianyunx86_64-linuxCloud VMTencent
focalorsx86_64-linuxVMParallels
nahidax86_64-linuxContainerLXC
wandererx86_64-linuxWSLWindows

lib.codgician Functions

System Builders (Always use these)

nix
# hosts/nixos/<name>/default.nix
{ inputs, ... }:
inputs.self.lib.codgician.mkNixosSystem {
  hostName = builtins.baseNameOf ./.;
}

# hosts/darwin/<name>/default.nix
inputs.self.lib.codgician.mkDarwinSystem {
  hostName = builtins.baseNameOf ./.;
}

Reverse Proxy Helpers

nix
# In options:
reverseProxy = lib.codgician.mkServiceReverseProxyOptions {
  serviceName = "myservice";
  defaultProxyPass = "http://127.0.0.1:8080";
};

# In config:
config = lib.mkMerge [
  (lib.mkIf cfg.enable { ... })
  (lib.codgician.mkServiceReverseProxyConfig { inherit serviceName cfg; })
];

Generated options: reverseProxy.enable, .domains, .lanOnly, .authelia.enable, .proxyPass

Other Helpers

FunctionPurpose
getFolderPaths/getFolderNamesAuto-discovery
getAgeSecretPathFromNameSecret name → path
mkServiceUserGroupLinuxCreate service user/group

Module Patterns

Option Namespace

nix
options.codgician.<category>.<name> = { ... };
# categories: services, system, users

Use config.codgician.* not config.services.*

Standard Service Module

nix
{ config, lib, pkgs, ... }:
let
  serviceName = "myservice";
  cfg = config.codgician.services.${serviceName};
in {
  options.codgician.services.${serviceName} = {
    enable = lib.mkEnableOption "My Service";
    user = lib.mkOption { type = lib.types.str; default = serviceName; };
    group = lib.mkOption { type = lib.types.str; default = serviceName; };
    reverseProxy = lib.codgician.mkServiceReverseProxyOptions {
      inherit serviceName;
      defaultProxyPass = "http://127.0.0.1:8080";
    };
  };

  config = lib.mkMerge [
    (lib.mkIf cfg.enable {
      # Wrap existing NixOS service or create systemd service
    })
    (lib.codgician.mkServiceReverseProxyConfig { inherit serviceName cfg; })
  ];
}

Secret Registration

nix
codgician.system.agenix.secrets = lib.genAttrs
  [ "${serviceName}-env" ]
  (name: { owner = cfg.user; group = cfg.group; mode = "0600"; });

# Use in service:
systemd.services.${serviceName}.serviceConfig.EnvironmentFile = 
  config.age.secrets."${serviceName}-env".path;

Impermanence

nix
codgician.system.impermanence.extraItems = [
  { type = "directory"; path = cfg.dataDir; inherit (cfg) user group; }
];

Commands

bash
# Build
nix build .#nixosConfigurations.<host>.config.system.build.toplevel
nix build .#darwinConfigurations.<host>.system

# Deploy (requires approval)
nixos-rebuild switch --flake .#<host> --target-host <host> --use-remote-sudo
darwin-rebuild switch --flake .#<host>

# Test
nixos-rebuild build-vm --flake .#<host>