nixos/modules/system/nix.nix

161 lines
5.3 KiB
Nix
Raw Normal View History

2023-09-02 14:04:03 +08:00
inputs:
{
options.nixos.system.nix = let inherit (inputs.lib) mkOption types; in
{
2023-09-02 14:21:27 +08:00
# marches allowed to be compiled on this machine
2023-09-02 14:04:03 +08:00
marches = mkOption { type = types.nullOr (types.listOf types.nonEmptyStr); default = null; };
2024-02-04 11:13:34 +08:00
includeBuildDependencies = mkOption { type = types.bool; default = inputs.topInputs.self.config.archive; };
2023-09-02 14:04:03 +08:00
substituters = mkOption { type = types.nullOr (types.listOf types.nonEmptyStr); default = null; };
autoOptimiseStore = mkOption { type = types.bool; default = false; };
2024-03-11 16:19:37 +08:00
remote =
{
slave =
{
enable = mkOption { type = types.bool; default = false; };
mandatoryFeatures = mkOption
{
type = types.listOf types.nonEmptyStr;
default = [ "big-parallel" ];
};
2024-03-11 16:19:37 +08:00
};
master =
{
enable = mkOption { type = types.bool; default = false; };
hosts = mkOption { type = types.listOf types.nonEmptyStr; default = []; };
};
};
2024-05-24 11:41:09 +08:00
githubToken.enable = mkOption { type = types.bool; default = false; };
2023-09-02 14:04:03 +08:00
};
2024-02-01 10:55:47 +08:00
config = let inherit (inputs.config.nixos.system) nix; in inputs.lib.mkMerge
[
# general nix config
{
nix.settings =
2023-09-02 14:04:03 +08:00
{
2024-02-01 10:55:47 +08:00
system-features = [ "big-parallel" "nixos-test" "benchmark" ];
2024-06-22 16:04:24 +08:00
experimental-features = [ "nix-command" "flakes" "ca-derivations" ];
2024-02-01 10:55:47 +08:00
keep-failed = true;
max-substitution-jobs = 4;
2024-02-01 10:55:47 +08:00
trusted-public-keys = [ "chn:Cc+nowW1LIpe1kyXOZmNaznFDiH1glXmpb4A+WD/DTE=" ];
2024-03-11 16:19:37 +08:00
trusted-users = [ "@wheel" ];
2024-02-01 10:55:47 +08:00
show-trace = true;
max-jobs = 4;
2024-02-01 10:55:47 +08:00
cores = 0;
keep-going = true;
2024-02-27 11:19:29 +08:00
keep-outputs = true;
2024-02-01 10:55:47 +08:00
};
systemd.services.nix-daemon = { serviceConfig.CacheDirectory = "nix"; environment.TMPDIR = "/var/cache/nix"; };
}
# nix daemon use lower io/cpu priority
{ nix = { daemonIOSchedClass = "idle"; daemonCPUSchedPolicy = "idle"; }; }
2024-02-01 10:55:47 +08:00
# nix channel & nix flake registry
{
nix =
{
registry =
2023-09-02 14:04:03 +08:00
{
2024-02-01 10:55:47 +08:00
nixpkgs.flake = inputs.topInputs.nixpkgs;
nixos.flake = inputs.topInputs.self;
2023-09-02 14:04:03 +08:00
};
2024-02-01 10:55:47 +08:00
nixPath = [ "nixpkgs=${inputs.topInputs.nixpkgs}" ];
};
environment =
{
etc =
2023-09-02 14:04:03 +08:00
{
2024-02-01 10:55:47 +08:00
"channels/nixpkgs".source = inputs.topInputs.nixpkgs.outPath;
"nixos".source = inputs.topInputs.self.outPath;
2023-09-02 14:04:03 +08:00
};
2024-06-23 11:32:57 +08:00
variables.COMMA_NIXPKGS_FLAKE = "nixpkgs";
2023-09-02 14:04:03 +08:00
};
2024-02-01 10:55:47 +08:00
}
# marches
{
nix.settings.system-features =
(map
2024-02-01 10:55:47 +08:00
(march: "gccarch-${march}")
(
if nix.marches == null then
(with inputs.config.nixos.system.nixpkgs; if march == null then [] else [ march ])
else nix.marches
))
++ (with inputs.config.nixos.system.nixpkgs; if march == null then [] else [ "gccarch-exact-${march}" ]);
2024-02-01 10:55:47 +08:00
}
# includeBuildDependencies
2024-03-11 16:19:37 +08:00
(inputs.lib.mkIf nix.includeBuildDependencies
2024-02-01 10:55:47 +08:00
{
system.includeBuildDependencies = nix.includeBuildDependencies;
2024-03-11 16:19:37 +08:00
})
2024-02-01 10:55:47 +08:00
# substituters
{
2024-09-13 20:01:00 +08:00
nix.settings.substituters = inputs.lib.mkIf (nix.substituters != null) nix.substituters;
2024-02-01 10:55:47 +08:00
}
# autoOptimiseStore
2024-03-11 16:19:37 +08:00
(inputs.lib.mkIf nix.autoOptimiseStore
2024-02-01 10:55:47 +08:00
{
nix.settings.auto-optimise-store = nix.autoOptimiseStore;
2024-03-11 16:19:37 +08:00
})
# remote.slave
(inputs.lib.mkIf nix.remote.slave.enable
{
nix =
{
sshServe =
{
enable = true;
keys = [ "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAdUiHbT1Vs++5L0OPaMtYG7Wa0ejbJs2KBZ4QAspM4n nix-ssh@pc" ];
write = true;
protocol = "ssh-ng";
};
settings.trusted-users = [ "nix-ssh" ];
};
})
# remote.master
(inputs.lib.mkIf nix.remote.master.enable
{
assertions = builtins.map
(host:
{
assertion = inputs.topInputs.self.nixosConfigurations.${host}.config.nixos.system.nix.remote.slave.enable;
message = "remote.slave.enable is not set for ${host}";
})
nix.remote.master.hosts;
nix =
{
distributedBuilds = true;
buildMachines = builtins.map
(host: let hostConfig = inputs.topInputs.self.nixosConfigurations.${host}.config; in
{
hostName = host;
protocol = "ssh-ng";
systems = [ "x86_64-linux" ] ++ hostConfig.nix.settings.extra-platforms or [];
2024-03-11 16:19:37 +08:00
sshUser = "nix-ssh";
sshKey = inputs.config.sops.secrets."nix/remote".path;
maxJobs = 1;
inherit (hostConfig.nixos.system.nix.remote.slave) mandatoryFeatures;
supportedFeatures = hostConfig.nix.settings.system-features;
})
nix.remote.master.hosts;
};
sops.secrets."nix/remote" = {};
})
2024-05-24 11:41:09 +08:00
(inputs.lib.mkIf nix.githubToken.enable
{
nix.extraOptions = "!include ${inputs.config.sops.templates."nix-github.conf".path}";
sops =
{
templates."nix-github.conf" =
{
content = "access-tokens = github.com=${inputs.config.sops.placeholder."github/token"}";
mode = "0444";
};
secrets."github/token" = {};
};
})
2024-02-01 10:55:47 +08:00
# c++ include path
# environment.pathsToLink = [ "/include" ];
# environment.variables.CPATH = "/run/current-system/sw/include";
# environment.variables.LIBRARY_PATH = "/run/current-system/sw/lib";
];
2023-09-02 14:04:03 +08:00
}