Refactor
This commit is contained in:
@@ -2,51 +2,48 @@
|
||||
let
|
||||
serverCfg = config.syscfg.server;
|
||||
builder = import ./builder.nix { inherit config lib pkgs serverCfg; };
|
||||
|
||||
in{
|
||||
config = lib.mkMerge [{
|
||||
syscfg.server.loadedContainers = lib.mapAttrs (name: containerCfg:
|
||||
builder.mkApp name ((import (./apps + "/${name}.nix")) { inherit config pkgs lib containerCfg builder name; })
|
||||
) config.syscfg.server.containers;
|
||||
} (lib.mkIf ( serverCfg.containers != {} ) (
|
||||
loadApp = name: containerCfg:
|
||||
builder.mkApp name ((import (./apps + "/${name}.nix")) {
|
||||
inherit config pkgs lib containerCfg builder name;
|
||||
});
|
||||
loadedContainers = lib.mapAttrs loadApp serverCfg.containers;
|
||||
appsList = builtins.attrValues loadedContainers;
|
||||
concatRuntimeLists = field: lib.concatMap (app: app.runtime.${field}) appsList;
|
||||
mkNamedUnits = mkUnit: items: lib.listToAttrs (map mkUnit items);
|
||||
mergedContainers = lib.concatMapAttrs (appName: app:
|
||||
lib.mapAttrs' (cName: cCfg: lib.nameValuePair "${appName}-${cName}" cCfg) app.runtime.containers
|
||||
) loadedContainers;
|
||||
allPathConfigs = map (path: {
|
||||
inherit path;
|
||||
mode = "0755";
|
||||
}) (lib.unique (builtins.attrValues serverCfg.path)) ++ concatRuntimeLists "paths";
|
||||
allSetupConfigs = map (app: ({ name = app.name; envFile = ""; } // app.runtime.setup)) appsList;
|
||||
allCronsConfigs = concatRuntimeLists "cron";
|
||||
allVMConfigs = builtins.filter (app: app.runtime.vm != null) appsList;
|
||||
mkPathSetup = cfg:
|
||||
let
|
||||
appsList = builtins.attrValues config.syscfg.server.loadedContainers;
|
||||
mergedContainers = lib.concatMapAttrs (appName: app:
|
||||
lib.mapAttrs' (cName: cCfg: lib.nameValuePair "${appName}-${cName}" cCfg) app.runtime.containers
|
||||
) config.syscfg.server.loadedContainers;
|
||||
serverPathConfigs = map (path: {
|
||||
inherit path;
|
||||
mode = "0755";
|
||||
}) (lib.unique (builtins.attrValues serverCfg.path));
|
||||
allPathConfigs = serverPathConfigs ++ lib.concatMap (app: app.runtime.paths) appsList;
|
||||
allSetupConfigs = lib.concatMap (app:
|
||||
if app.runtime.setup ? script
|
||||
then [ ({ name = app.name; envFile = ""; } // app.runtime.setup) ]
|
||||
else [ ]
|
||||
) appsList;
|
||||
allCronsConfigs = lib.concatMap (app: app.runtime.cron) appsList;
|
||||
allVMConfigs = builtins.filter (app: app.runtime.vm != null) appsList;
|
||||
in{
|
||||
effectiveCfg = {
|
||||
owner = "root:root";
|
||||
mode = "0400";
|
||||
dirs = [];
|
||||
} // cfg;
|
||||
in ''
|
||||
${pkgs.coreutils}/bin/mkdir -p "${effectiveCfg.path}"
|
||||
${lib.concatMapStringsSep "\n" (dir: "${pkgs.coreutils}/bin/mkdir -p ${effectiveCfg.path}/${lib.escapeShellArg dir}") effectiveCfg.dirs}
|
||||
${pkgs.coreutils}/bin/chown ${effectiveCfg.owner} "${effectiveCfg.path}"
|
||||
${pkgs.coreutils}/bin/chmod ${effectiveCfg.mode} "${effectiveCfg.path}"
|
||||
'';
|
||||
in {
|
||||
config = lib.mkMerge [{
|
||||
syscfg.server.loadedContainers = loadedContainers;
|
||||
} (lib.mkIf (loadedContainers != {}) {
|
||||
virtualisation.oci-containers = {
|
||||
backend = "podman";
|
||||
containers = mergedContainers;
|
||||
};
|
||||
system.activationScripts.container-setup-dirs = {
|
||||
deps = [ "users" "groups" ];
|
||||
text = lib.concatStringsSep "\n" (map (cfg:
|
||||
let
|
||||
effectiveCfg = {
|
||||
owner = "root:root";
|
||||
mode = "0400";
|
||||
dirs = [];
|
||||
} // cfg;
|
||||
in ''
|
||||
${pkgs.coreutils}/bin/mkdir -p "${effectiveCfg.path}"
|
||||
${lib.concatMapStringsSep "\n" (dir: "${pkgs.coreutils}/bin/mkdir -p ${effectiveCfg.path}/${lib.escapeShellArg dir}") effectiveCfg.dirs}
|
||||
${pkgs.coreutils}/bin/chown ${effectiveCfg.owner} "${effectiveCfg.path}"
|
||||
${pkgs.coreutils}/bin/chmod ${effectiveCfg.mode} "${effectiveCfg.path}"
|
||||
|
||||
'') allPathConfigs);
|
||||
text = lib.concatStringsSep "\n" (map mkPathSetup allPathConfigs);
|
||||
};
|
||||
|
||||
systemd.services = {
|
||||
@@ -60,7 +57,7 @@ in{
|
||||
startAt = "weekly";
|
||||
};
|
||||
}
|
||||
// lib.listToAttrs (lib.concatMap (e: [{
|
||||
// mkNamedUnits (e: {
|
||||
name = "${e.name}-vm";
|
||||
value = {
|
||||
description = "Isolated NixOS Guest VM for ${e.name}";
|
||||
@@ -81,8 +78,8 @@ in{
|
||||
'';
|
||||
};
|
||||
};
|
||||
}]) allVMConfigs)
|
||||
// lib.listToAttrs (lib.concatMap (e: [{
|
||||
}) allVMConfigs
|
||||
// mkNamedUnits (e: {
|
||||
name = "${e.name}-setup";
|
||||
value = {
|
||||
description = "Run ${e.name} setup";
|
||||
@@ -98,13 +95,11 @@ in{
|
||||
User = "root";
|
||||
};
|
||||
};
|
||||
}]) allSetupConfigs );
|
||||
}) allSetupConfigs;
|
||||
|
||||
services.cron = {
|
||||
enable = true;
|
||||
systemCronJobs = allCronsConfigs;
|
||||
};
|
||||
|
||||
}))];
|
||||
|
||||
})];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user