2024-03-17 18:48:49 +00:00
|
|
|
{
|
|
|
|
clan-core,
|
|
|
|
nixpkgs,
|
|
|
|
lib,
|
|
|
|
}:
|
|
|
|
{
|
|
|
|
directory, # The directory containing the machines subdirectory
|
|
|
|
specialArgs ? { }, # Extra arguments to pass to nixosSystem i.e. useful to make self available
|
|
|
|
machines ? { }, # allows to include machine-specific modules i.e. machines.${name} = { ... }
|
|
|
|
clanName, # Needs to be (globally) unique, as this determines the folder name where the flake gets downloaded to.
|
|
|
|
clanIcon ? null, # A path to an icon to be used for the clan, should be the same for all machines
|
|
|
|
pkgsForSystem ? (_system: null), # A map from arch to pkgs, if specified this nixpkgs will be only imported once for each system.
|
|
|
|
# This improves performance, but all nipxkgs.* options will be ignored.
|
2023-08-29 13:00:03 +00:00
|
|
|
}:
|
|
|
|
let
|
2024-03-17 18:48:49 +00:00
|
|
|
machinesDirs = lib.optionalAttrs (builtins.pathExists "${directory}/machines") (
|
|
|
|
builtins.readDir (directory + /machines)
|
|
|
|
);
|
2023-08-29 13:00:03 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
machineSettings =
|
|
|
|
machineName:
|
2023-10-25 15:36:01 +00:00
|
|
|
# CLAN_MACHINE_SETTINGS_FILE allows to override the settings file temporarily
|
|
|
|
# This is useful for doing a dry-run before writing changes into the settings.json
|
|
|
|
# Using CLAN_MACHINE_SETTINGS_FILE requires passing --impure to nix eval
|
2024-03-17 18:48:49 +00:00
|
|
|
if builtins.getEnv "CLAN_MACHINE_SETTINGS_FILE" != "" then
|
|
|
|
builtins.fromJSON (builtins.readFile (builtins.getEnv "CLAN_MACHINE_SETTINGS_FILE"))
|
2023-10-24 17:40:48 +00:00
|
|
|
else
|
2024-03-17 18:48:49 +00:00
|
|
|
lib.optionalAttrs (builtins.pathExists "${directory}/machines/${machineName}/settings.json") (
|
|
|
|
builtins.fromJSON (builtins.readFile (directory + /machines/${machineName}/settings.json))
|
|
|
|
);
|
2023-08-29 13:00:03 +00:00
|
|
|
|
2023-10-25 15:36:01 +00:00
|
|
|
# Read additional imports specified via a config option in settings.json
|
|
|
|
# This is not an infinite recursion, because the imports are discovered here
|
|
|
|
# before calling evalModules.
|
|
|
|
# It is still useful to have the imports as an option, as this allows for type
|
|
|
|
# checking and easy integration with the config frontend(s)
|
2024-03-17 18:48:49 +00:00
|
|
|
machineImports =
|
|
|
|
machineSettings: map (module: clan-core.clanModules.${module}) (machineSettings.clanImports or [ ]);
|
2023-10-25 15:36:01 +00:00
|
|
|
|
2023-09-28 12:13:23 +00:00
|
|
|
# TODO: remove default system once we have a hardware-config mechanism
|
2024-03-17 18:48:49 +00:00
|
|
|
nixosConfiguration =
|
|
|
|
{
|
|
|
|
system ? "x86_64-linux",
|
|
|
|
name,
|
|
|
|
pkgs ? null,
|
|
|
|
extraConfig ? { },
|
|
|
|
}:
|
|
|
|
nixpkgs.lib.nixosSystem {
|
|
|
|
modules =
|
|
|
|
let
|
|
|
|
settings = machineSettings name;
|
|
|
|
in
|
|
|
|
(machineImports settings)
|
|
|
|
++ [
|
|
|
|
settings
|
|
|
|
clan-core.nixosModules.clanCore
|
|
|
|
extraConfig
|
|
|
|
(machines.${name} or { })
|
|
|
|
(
|
|
|
|
{
|
|
|
|
clanCore.clanName = clanName;
|
|
|
|
clanCore.clanIcon = clanIcon;
|
|
|
|
clanCore.clanDir = directory;
|
|
|
|
clanCore.machineName = name;
|
|
|
|
nixpkgs.hostPlatform = lib.mkDefault system;
|
2023-11-16 12:49:35 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
# speeds up nix commands by using the nixpkgs from the host system (especially useful in VMs)
|
|
|
|
nix.registry.nixpkgs.to = {
|
|
|
|
type = "path";
|
|
|
|
path = lib.mkDefault nixpkgs;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// lib.optionalAttrs (pkgs != null) { nixpkgs.pkgs = lib.mkForce pkgs; }
|
|
|
|
)
|
|
|
|
];
|
2024-04-02 09:03:45 +00:00
|
|
|
specialArgs = {
|
|
|
|
inherit clan-core;
|
|
|
|
} // specialArgs;
|
2024-03-17 18:48:49 +00:00
|
|
|
};
|
2023-09-20 16:08:47 +00:00
|
|
|
|
2023-09-27 15:25:17 +00:00
|
|
|
allMachines = machinesDirs // machines;
|
2023-09-20 16:08:47 +00:00
|
|
|
|
2023-09-27 15:25:17 +00:00
|
|
|
supportedSystems = [
|
2023-09-20 16:08:47 +00:00
|
|
|
"x86_64-linux"
|
|
|
|
"aarch64-linux"
|
|
|
|
"riscv64-linux"
|
|
|
|
"x86_64-darwin"
|
|
|
|
"aarch64-darwin"
|
|
|
|
];
|
|
|
|
|
2023-09-27 15:25:17 +00:00
|
|
|
nixosConfigurations = lib.mapAttrs (name: _: nixosConfiguration { inherit name; }) allMachines;
|
|
|
|
|
|
|
|
# This instantiates nixos for each system that we support:
|
2023-09-27 15:25:17 +00:00
|
|
|
# configPerSystem = <system>.<machine>.nixosConfiguration
|
2023-09-27 15:25:17 +00:00
|
|
|
# We need this to build nixos secret generators for each system
|
2024-03-17 18:48:49 +00:00
|
|
|
configsPerSystem = builtins.listToAttrs (
|
|
|
|
builtins.map (
|
|
|
|
system:
|
|
|
|
lib.nameValuePair system (
|
|
|
|
lib.mapAttrs (
|
|
|
|
name: _:
|
|
|
|
nixosConfiguration {
|
2024-02-21 09:44:00 +00:00
|
|
|
inherit name system;
|
|
|
|
pkgs = pkgsForSystem system;
|
2024-03-17 18:48:49 +00:00
|
|
|
}
|
|
|
|
) allMachines
|
|
|
|
)
|
|
|
|
) supportedSystems
|
|
|
|
);
|
2024-02-10 10:47:09 +00:00
|
|
|
|
2024-03-17 18:48:49 +00:00
|
|
|
configsFuncPerSystem = builtins.listToAttrs (
|
|
|
|
builtins.map (
|
|
|
|
system:
|
|
|
|
lib.nameValuePair system (
|
|
|
|
lib.mapAttrs (
|
|
|
|
name: _: args:
|
|
|
|
nixosConfiguration (
|
|
|
|
args
|
|
|
|
// {
|
|
|
|
inherit name system;
|
|
|
|
pkgs = pkgsForSystem system;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
) allMachines
|
|
|
|
)
|
|
|
|
) supportedSystems
|
|
|
|
);
|
2023-09-27 15:25:17 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
inherit nixosConfigurations;
|
|
|
|
|
2023-09-20 16:08:47 +00:00
|
|
|
clanInternals = {
|
2023-09-29 09:56:02 +00:00
|
|
|
machines = configsPerSystem;
|
2024-02-10 10:47:09 +00:00
|
|
|
machinesFunc = configsFuncPerSystem;
|
2024-03-17 18:48:49 +00:00
|
|
|
all-machines-json = lib.mapAttrs (
|
|
|
|
system: configs:
|
|
|
|
nixpkgs.legacyPackages.${system}.writers.writeJSON "machines.json" (
|
|
|
|
lib.mapAttrs (_: m: m.config.system.clan.deployment.data) configs
|
|
|
|
)
|
|
|
|
) configsPerSystem;
|
2023-09-20 16:08:47 +00:00
|
|
|
};
|
2023-09-27 15:25:17 +00:00
|
|
|
}
|