Inventory: simplify build-clan interface
This commit is contained in:
parent
f5a0fd7c9e
commit
c509248d05
@ -27,8 +27,50 @@ let
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
# Each .nix file in the roles directory is a role
|
||||||
|
# TODO: Helper function to set available roles within module meta.
|
||||||
|
# roles = lib.pipe ./roles [
|
||||||
|
# (p: if builtins.pathExists p then p else throw "Role directory does not exist")
|
||||||
|
# builtins.readDir
|
||||||
|
# (lib.filterAttrs (_n: v: v == "regular"))
|
||||||
|
# lib.attrNames
|
||||||
|
# (map (fileName: lib.removeSuffix ".nix" fileName))
|
||||||
|
# ];
|
||||||
|
|
||||||
|
# TODO: make this an interface of every module
|
||||||
|
# Maybe load from readme.md
|
||||||
|
metaInfoOption = lib.mkOption {
|
||||||
|
readOnly = true;
|
||||||
|
default = {
|
||||||
|
description = "A category for the service. This is used to group services in the clan ui";
|
||||||
|
};
|
||||||
|
type = lib.types.submodule {
|
||||||
|
options = {
|
||||||
|
description = lib.mkOption {
|
||||||
|
description = "A category for the service. This is used to group services in the clan ui";
|
||||||
|
type = lib.types.str;
|
||||||
|
};
|
||||||
|
# icon = lib.mkOption {
|
||||||
|
# description = "A category for the service. This is used to group services in the clan ui";
|
||||||
|
# type = lib.types.str;
|
||||||
|
# };
|
||||||
|
# screenshots = lib.mkOption {
|
||||||
|
# description = "A category for the service. This is used to group services in the clan ui";
|
||||||
|
# type = lib.types.str;
|
||||||
|
# };
|
||||||
|
# category = lib.mkOption {
|
||||||
|
# description = "A category for the service. This is used to group services in the clan ui";
|
||||||
|
# type = lib.types.enum ["backup" "network"];
|
||||||
|
# };
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
|
|
||||||
|
options.clan.borgbackup.meta = metaInfoOption;
|
||||||
|
|
||||||
options.clan.borgbackup.destinations = lib.mkOption {
|
options.clan.borgbackup.destinations = lib.mkOption {
|
||||||
type = lib.types.attrsOf (
|
type = lib.types.attrsOf (
|
||||||
lib.types.submodule (
|
lib.types.submodule (
|
||||||
@ -63,6 +105,11 @@ in
|
|||||||
};
|
};
|
||||||
|
|
||||||
imports = [
|
imports = [
|
||||||
|
{
|
||||||
|
config.clan.borgbackup.meta = {
|
||||||
|
description = "backup";
|
||||||
|
};
|
||||||
|
}
|
||||||
(lib.mkRemovedOptionModule [
|
(lib.mkRemovedOptionModule [
|
||||||
"clan"
|
"clan"
|
||||||
"borgbackup"
|
"borgbackup"
|
||||||
@ -76,7 +123,7 @@ in
|
|||||||
lib.nameValuePair "borgbackup-job-${dest.name}" {
|
lib.nameValuePair "borgbackup-job-${dest.name}" {
|
||||||
# since borgbackup mounts the system read-only, we need to run in a ExecStartPre script, so we can generate additional files.
|
# since borgbackup mounts the system read-only, we need to run in a ExecStartPre script, so we can generate additional files.
|
||||||
serviceConfig.ExecStartPre = [
|
serviceConfig.ExecStartPre = [
|
||||||
(''+${pkgs.writeShellScript "borgbackup-job-${dest.name}-pre-backup-commands" preBackupScript}'')
|
''+${pkgs.writeShellScript "borgbackup-job-${dest.name}-pre-backup-commands" preBackupScript}''
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
) cfg.destinations;
|
) cfg.destinations;
|
||||||
|
@ -4,22 +4,18 @@ let
|
|||||||
in
|
in
|
||||||
{
|
{
|
||||||
clan = clan-core.lib.buildClan {
|
clan = clan-core.lib.buildClan {
|
||||||
|
|
||||||
meta.name = "kenjis clan";
|
meta.name = "kenjis clan";
|
||||||
# Should usually point to the directory of flake.nix
|
# Should usually point to the directory of flake.nix
|
||||||
directory = self;
|
directory = self;
|
||||||
|
|
||||||
# service config
|
inventory = {
|
||||||
# Useful alias: "inventory.services.borgbackup.default"
|
|
||||||
services = {
|
services = {
|
||||||
borgbackup = {
|
borgbackup.instance_1 = {
|
||||||
roles.server.machines = [ "vyr_machine" ];
|
roles.server.machines = [ "vyr_machine" ];
|
||||||
roles.client.tags = [ "laptop" ];
|
roles.client.tags = [ "laptop" ];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
# merged with
|
|
||||||
inventory = builtins.fromJSON (builtins.readFile ./src/tests/borgbackup.json);
|
|
||||||
|
|
||||||
# merged with
|
# merged with
|
||||||
machines = {
|
machines = {
|
||||||
|
@ -15,15 +15,6 @@
|
|||||||
# A map from arch to pkgs, if specified this nixpkgs will be only imported once for each system.
|
# 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.
|
# This improves performance, but all nipxkgs.* options will be ignored.
|
||||||
pkgsForSystem ? (_system: null),
|
pkgsForSystem ? (_system: null),
|
||||||
/*
|
|
||||||
Distributed services configuration.
|
|
||||||
|
|
||||||
This configures a default instance in the inventory with the name "default".
|
|
||||||
|
|
||||||
If you need multiple instances of a service configure them via:
|
|
||||||
inventory.services.[serviceName].[instanceName] = ...
|
|
||||||
*/
|
|
||||||
services ? { },
|
|
||||||
/*
|
/*
|
||||||
Low level inventory configuration.
|
Low level inventory configuration.
|
||||||
Overrides the services configuration.
|
Overrides the services configuration.
|
||||||
@ -44,15 +35,15 @@ let
|
|||||||
modules = [
|
modules = [
|
||||||
./interface.nix
|
./interface.nix
|
||||||
{ inherit meta; }
|
{ inherit meta; }
|
||||||
# Default instances configured via 'services'
|
(
|
||||||
{
|
if
|
||||||
services = lib.mapAttrs (_name: value: {
|
builtins.pathExists "${directory}/inventory.json"
|
||||||
default = value // {
|
# Is recursively applied. Any explicit nix will override.
|
||||||
meta.name = lib.mkDefault _name;
|
then
|
||||||
};
|
lib.mkDefault (builtins.fromJSON (builtins.readFile "${directory}/inventory.json"))
|
||||||
}) services;
|
else
|
||||||
}
|
{ }
|
||||||
# The inventory overrides
|
)
|
||||||
inventory
|
inventory
|
||||||
# Machines explicitly configured via 'machines' argument
|
# Machines explicitly configured via 'machines' argument
|
||||||
{
|
{
|
||||||
@ -79,13 +70,17 @@ let
|
|||||||
}
|
}
|
||||||
) machines;
|
) machines;
|
||||||
}
|
}
|
||||||
# Machines that exist in the machines directory
|
|
||||||
{ machines = lib.mapAttrs (name: _: { inherit name; }) machinesDirs; }
|
# Deprecated interface
|
||||||
|
(if clanName != null then { meta.name = clanName; } else { })
|
||||||
|
(if clanIcon != null then { meta.icon = clanIcon; } else { })
|
||||||
];
|
];
|
||||||
}).config;
|
}).config;
|
||||||
|
|
||||||
buildInventory = import ./inventory.nix { inherit lib clan-core; };
|
buildInventory = import ./inventory.nix { inherit lib clan-core; };
|
||||||
|
|
||||||
|
# map from machine name to service configuration
|
||||||
|
# { ${machineName} :: Config }
|
||||||
serviceConfigs = buildInventory mergedInventory;
|
serviceConfigs = buildInventory mergedInventory;
|
||||||
|
|
||||||
deprecationWarnings = [
|
deprecationWarnings = [
|
||||||
@ -95,62 +90,6 @@ let
|
|||||||
(lib.warnIf (clanIcon != null) "clanIcon is deprecated, please use meta.icon instead" null)
|
(lib.warnIf (clanIcon != null) "clanIcon is deprecated, please use meta.icon instead" null)
|
||||||
];
|
];
|
||||||
|
|
||||||
machinesDirs = lib.optionalAttrs (builtins.pathExists "${directory}/machines") (
|
|
||||||
builtins.readDir (directory + /machines)
|
|
||||||
);
|
|
||||||
|
|
||||||
mergedMeta =
|
|
||||||
let
|
|
||||||
metaFromFile =
|
|
||||||
if (builtins.pathExists "${directory}/clan/meta.json") then
|
|
||||||
let
|
|
||||||
settings = builtins.fromJSON (builtins.readFile "${directory}/clan/meta.json");
|
|
||||||
in
|
|
||||||
settings
|
|
||||||
else
|
|
||||||
{ };
|
|
||||||
legacyMeta = lib.filterAttrs (_: v: v != null) {
|
|
||||||
name = clanName;
|
|
||||||
icon = clanIcon;
|
|
||||||
};
|
|
||||||
optionsMeta = lib.filterAttrs (_: v: v != null) meta;
|
|
||||||
|
|
||||||
warnings =
|
|
||||||
builtins.map (
|
|
||||||
name:
|
|
||||||
if
|
|
||||||
metaFromFile.${name} or null != optionsMeta.${name} or null && optionsMeta.${name} or null != null
|
|
||||||
then
|
|
||||||
lib.warn "meta.${name} is set in different places. (exlicit option meta.${name} overrides ${directory}/clan/meta.json)" null
|
|
||||||
else
|
|
||||||
null
|
|
||||||
) (builtins.attrNames metaFromFile)
|
|
||||||
++ [ (if (res.name or null == null) then (throw "meta.name should be set") else null) ];
|
|
||||||
res = metaFromFile // legacyMeta // optionsMeta;
|
|
||||||
in
|
|
||||||
# Print out warnings before returning the merged result
|
|
||||||
builtins.deepSeq warnings res;
|
|
||||||
|
|
||||||
machineSettings =
|
|
||||||
machineName:
|
|
||||||
# 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
|
|
||||||
if builtins.getEnv "CLAN_MACHINE_SETTINGS_FILE" != "" then
|
|
||||||
builtins.fromJSON (builtins.readFile (builtins.getEnv "CLAN_MACHINE_SETTINGS_FILE"))
|
|
||||||
else
|
|
||||||
lib.optionalAttrs (builtins.pathExists "${directory}/machines/${machineName}/settings.json") (
|
|
||||||
builtins.fromJSON (builtins.readFile (directory + /machines/${machineName}/settings.json))
|
|
||||||
);
|
|
||||||
|
|
||||||
# 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)
|
|
||||||
machineImports =
|
|
||||||
machineSettings: map (module: clan-core.clanModules.${module}) (machineSettings.clanImports or [ ]);
|
|
||||||
|
|
||||||
# TODO: remove default system once we have a hardware-config mechanism
|
# TODO: remove default system once we have a hardware-config mechanism
|
||||||
nixosConfiguration =
|
nixosConfiguration =
|
||||||
{
|
{
|
||||||
@ -160,13 +99,7 @@ let
|
|||||||
extraConfig ? { },
|
extraConfig ? { },
|
||||||
}:
|
}:
|
||||||
nixpkgs.lib.nixosSystem {
|
nixpkgs.lib.nixosSystem {
|
||||||
modules =
|
modules = [
|
||||||
let
|
|
||||||
settings = machineSettings name;
|
|
||||||
in
|
|
||||||
(machineImports settings)
|
|
||||||
++ [
|
|
||||||
settings
|
|
||||||
clan-core.nixosModules.clanCore
|
clan-core.nixosModules.clanCore
|
||||||
extraConfig
|
extraConfig
|
||||||
(machines.${name} or { })
|
(machines.${name} or { })
|
||||||
@ -200,7 +133,7 @@ let
|
|||||||
} // specialArgs;
|
} // specialArgs;
|
||||||
};
|
};
|
||||||
|
|
||||||
allMachines = machinesDirs // machines;
|
allMachines = mergedInventory.machines or { };
|
||||||
|
|
||||||
supportedSystems = [
|
supportedSystems = [
|
||||||
"x86_64-linux"
|
"x86_64-linux"
|
||||||
@ -252,11 +185,11 @@ builtins.deepSeq deprecationWarnings {
|
|||||||
inherit nixosConfigurations;
|
inherit nixosConfigurations;
|
||||||
|
|
||||||
clanInternals = {
|
clanInternals = {
|
||||||
# Evaluated clan meta
|
meta = mergedInventory.meta;
|
||||||
# Merged /clan/meta.json with overrides from buildClan
|
|
||||||
meta = mergedMeta;
|
|
||||||
inventory = mergedInventory;
|
inventory = mergedInventory;
|
||||||
|
|
||||||
|
invFile = "${directory}/inventory.json";
|
||||||
|
|
||||||
# machine specifics
|
# machine specifics
|
||||||
machines = configsPerSystem;
|
machines = configsPerSystem;
|
||||||
machinesFunc = configsFuncPerSystem;
|
machinesFunc = configsFuncPerSystem;
|
||||||
|
Loading…
Reference in New Issue
Block a user