diff --git a/default.nix b/default.nix index a456110..1e064a6 100644 --- a/default.nix +++ b/default.nix @@ -25,7 +25,13 @@ let eval = evalModules { modules = [ - { + ({ lib, pkgs, ... }: { + _module.args = { + utils = import "${nixpkgs}/nixos/lib/utils.nix" { + inherit lib pkgs; + config.systemd.globalEnvironment = {}; + }; + }; nixpkgs = { source = nixpkgs; overlays = [ overlay ]; @@ -33,7 +39,7 @@ let "python-2.7.18.8" ]; }; - } + }) device.module liminix-config ]; diff --git a/modules/base.nix b/modules/base.nix index 30141a8..257200e 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -2,7 +2,7 @@ ## ============ -{ lib, pkgs, config, ...}: +{ lib, pkgs, utils, config, ...}: let inherit (lib) mkEnableOption mkOption types isDerivation hasAttr concatStringsSep mapAttrsToList; inherit (pkgs.pseudofile) dir symlink; @@ -43,9 +43,15 @@ in { ''; }; }; + + # deprecated services = mkOption { type = types.attrsOf type_service; }; + + units = mkOption { + type = utils.systemdUtils.types.units; + }; system.callService = mkOption { type = types.functionTo (types.functionTo types.anything); }; diff --git a/modules/systemd/default.nix b/modules/systemd/default.nix index 7b28730..17cf0cc 100644 --- a/modules/systemd/default.nix +++ b/modules/systemd/default.nix @@ -1,41 +1,120 @@ -{ pkgs, ... }: +{ + pkgs, + lib, + config, + utils, + ... +}: let + inherit (lib) + mapAttrs' + nameValuePair + mkMerge + mapAttrsToList + ; inherit (pkgs.pseudofile) dir symlink; - "getty.service" = { - file = - let - login = pkgs.writeScript "login" '' - #!/bin/ash - exec /bin/ash - ''; - in - '' - [Unit] - Description="Serial shell" + inherit (utils.systemdUtils.lib) + targetToUnit + serviceToUnit + ; + + units-texts = mapAttrs' ( + _: unit: + nameValuePair unit.name { + file = unit.text; + mode = "0644"; + } + ) config.units; + units-aliases = mkMerge ( + mapAttrsToList ( + _: unit: + mkMerge ( + map (aka: { + ${aka} = symlink "${unit.name}"; + }) (unit.aliases or [ ]) + ) + ) config.units + ); + units-extraWants = mkMerge ( + mapAttrsToList ( + _: unit: + mkMerge ( + map (unit2: { + "${unit2}.wants" = dir { + ${unit.name} = symlink "../${unit.name}"; + }; + }) (unit.wantedBy or [ ]) + ) + ) config.units + ); + units-extraUpholds = mkMerge ( + mapAttrsToList ( + _: unit: + mkMerge ( + map (unit2: { + "${unit2}.upholds" = dir { + ${unit.name} = symlink "../${unit.name}"; + }; + }) (unit.upheldBy or [ ]) + ) + ) config.units + ); + units-extraRequires = mkMerge ( + mapAttrsToList ( + _: unit: + mkMerge ( + map (unit2: { + "${unit2}.requires" = dir { + ${unit.name} = symlink "../${unit.name}"; + }; + }) (unit.requiredBy or [ ]) + ) + ) config.units + ); - [Service] - ExecStart=${pkgs.util-linux}/bin/agetty --login-program ${login} ttyS0 - ''; - mode = "0644"; - }; - "default.target" = { - file = '' - [Unit] - Description="target to boot" - Wants=getty.service - After=getty.service - AllowIsolate=yes - ''; - }; - "sysinit.target" = { - file = '' - [Unit] - Description="sysinit.target" - ''; - }; in { config = { + units = { + default-target = targetToUnit { + name = "default.target"; + aliases = []; + wantedBy = []; + requiredBy = []; + upheldBy = []; + unitConfig.Description = "target to boot"; + }; + sysinit-target = targetToUnit { + name = "sysinit.target"; + aliases = []; + wantedBy = []; + requiredBy = []; + upheldBy = []; + unitConfig.Description = "sysinit.target"; + }; + agetty = serviceToUnit { + name = "getty.service"; + aliases = []; + wantedBy = [ "default.target" ]; + requiredBy = []; + upheldBy = []; + environment = {}; + unitConfig = { + Description = "Serial Shell"; + Before = [ "default.target" ]; + }; + serviceConfig.ExecStart = + let + login = pkgs.writeScript "login" '' + #!/bin/ash + . /etc/profile + exec /bin/ash + ''; + in + "${pkgs.util-linux}/bin/agetty --login-program ${login} ttyS0"; + }; + }; + kernel.config = { CGROUPS = "y"; DEVTMPFS = "y"; @@ -55,9 +134,13 @@ in filesystem = dir { etc = dir { systemd = dir { - system = dir { - inherit "default.target" "getty.service" "sysinit.target"; - }; + system = dir (mkMerge [ + units-texts + units-aliases + units-extraWants + units-extraUpholds + units-extraRequires + ]); }; }; bin = dir {