forked from clan/clan-core
clan template: reduce autogenerate code to a minimum
This commit is contained in:
parent
bf19e51c9f
commit
a4e182debf
28
lib/build-clan/default.nix
Normal file
28
lib/build-clan/default.nix
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{ nixpkgs, clan, 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} = { ... }
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
machinesDirs =
|
||||||
|
if builtins.pathExists (directory + /machines)
|
||||||
|
then builtins.readDir (directory + /machines)
|
||||||
|
else { };
|
||||||
|
|
||||||
|
machineSettings = machineName:
|
||||||
|
if builtins.pathExists (directory + /machines/${machineName}/settings.json)
|
||||||
|
then builtins.fromJSON (builtins.readFile (directory + /machines/${machineName}/settings.json))
|
||||||
|
else { };
|
||||||
|
|
||||||
|
nixosConfigurations = lib.mapAttrs
|
||||||
|
(name: _mod:
|
||||||
|
nixpkgs.lib.nixosSystem {
|
||||||
|
modules = [
|
||||||
|
(machineSettings name)
|
||||||
|
(machines.${name} or { })
|
||||||
|
] ++ lib.attrValues clan.clanModules;
|
||||||
|
specialArgs = specialArgs;
|
||||||
|
})
|
||||||
|
machinesDirs;
|
||||||
|
in
|
||||||
|
nixosConfigurations
|
@ -1,4 +1,4 @@
|
|||||||
{ lib, ... }:
|
{ lib, clan, nixpkgs, ... }:
|
||||||
{
|
{
|
||||||
findNixFiles = folder:
|
findNixFiles = folder:
|
||||||
lib.mapAttrs'
|
lib.mapAttrs'
|
||||||
@ -13,4 +13,6 @@
|
|||||||
(builtins.readDir folder);
|
(builtins.readDir folder);
|
||||||
|
|
||||||
jsonschema = import ./jsonschema { inherit lib; };
|
jsonschema = import ./jsonschema { inherit lib; };
|
||||||
|
|
||||||
|
buildClan = import ./build-clan { inherit lib clan nixpkgs; };
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
{ lib
|
{ lib
|
||||||
|
, inputs
|
||||||
, ...
|
, ...
|
||||||
}: {
|
}: {
|
||||||
imports = [
|
imports = [
|
||||||
./jsonschema/flake-module.nix
|
./jsonschema/flake-module.nix
|
||||||
];
|
];
|
||||||
flake.lib = import ./default.nix { inherit lib; };
|
flake.lib = import ./default.nix {
|
||||||
|
inherit lib;
|
||||||
|
inherit (inputs) clan nixpkgs;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
# AUTOMATICALLY GENERATED by clan
|
|
||||||
# TODO: move this code to a flakeModule in clan-core and import it via flake input
|
|
||||||
{ self, lib, inputs, ... }:
|
|
||||||
let
|
|
||||||
evalConfig = import (self.inputs.nixpkgs + /lib/eval-config.nix);
|
|
||||||
|
|
||||||
evalMachine = machineModule: evalConfig {
|
|
||||||
modules = [ machineModule ];
|
|
||||||
};
|
|
||||||
|
|
||||||
machinesDirs =
|
|
||||||
if builtins.pathExists ./machines
|
|
||||||
then builtins.readDir ./machines
|
|
||||||
else { };
|
|
||||||
|
|
||||||
machineSettings = machineName:
|
|
||||||
if builtins.pathExists ./machines/${machineName}/settings.json
|
|
||||||
then (builtins.fromJSON (builtins.readFile ./machines/${machineName}/settings.json))
|
|
||||||
else { };
|
|
||||||
|
|
||||||
# load all clan modules by default
|
|
||||||
machineModule = machineName: {
|
|
||||||
imports =
|
|
||||||
(lib.attrValues inputs.clan.clanModules)
|
|
||||||
++ [ (machineSettings machineName) ];
|
|
||||||
};
|
|
||||||
|
|
||||||
# autoamtically generate machine modules for all directories under ./machines
|
|
||||||
machineModulesGenerated =
|
|
||||||
lib.flip lib.mapAttrs' machinesDirs (name: _: {
|
|
||||||
name = "machine-${name}";
|
|
||||||
value = machineModule name;
|
|
||||||
});
|
|
||||||
|
|
||||||
# re-read the machine modules from the flake outputs, to allow amchines being
|
|
||||||
# defined by custom mechanisms
|
|
||||||
machineModules =
|
|
||||||
lib.filterAttrs (name: _: lib.hasPrefix "machine-" name) self.nixosModules;
|
|
||||||
|
|
||||||
in
|
|
||||||
{
|
|
||||||
flake.nixosModules = machineModulesGenerated;
|
|
||||||
# generate nixosConfigurations for all machines under self.nixosModules.machines-{machine_name}
|
|
||||||
flake.nixosConfigurations =
|
|
||||||
lib.mapAttrs (_: mod: evalMachine mod) machineModules;
|
|
||||||
}
|
|
@ -1,16 +1,11 @@
|
|||||||
{
|
{
|
||||||
description = "<Put your description here>";
|
description = "<Put your description here>";
|
||||||
|
|
||||||
inputs = {
|
inputs.clan-core.url = "git+https://git.clan.lol/clan/clan-core";
|
||||||
clan-core.url = "git+https://git.clan.lol/clan/clan-core";
|
|
||||||
flake-parts.url = "github:hercules-ci/flake-parts";
|
|
||||||
};
|
|
||||||
|
|
||||||
outputs = inputs @ { flake-parts, ... }:
|
outputs = { clan-core, ... }: {
|
||||||
flake-parts.lib.mkFlake { inherit inputs; } {
|
nixosConfigurations = clan-core.lib.buildClan {
|
||||||
systems = [ "x86_64-linux" ];
|
directory = ./.;
|
||||||
imports = [
|
|
||||||
./clan-flake-module.nix
|
|
||||||
];
|
|
||||||
};
|
};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@ -1 +0,0 @@
|
|||||||
["x86_64-linux"]
|
|
Loading…
Reference in New Issue
Block a user