{ pkgs, lib, config, utils, ... }: let inherit (lib) mapAttrs' nameValuePair mkMerge mapAttrsToList mkOption ; inherit (pkgs.pseudofile) dir symlink; inherit (utils.systemdUtils.lib) targetToUnit serviceToUnit ; systemd-types = import ./types.nix { inherit pkgs utils lib; busybox = config.programs.busybox.package; }; units-texts = mapAttrs' ( _: unit: nameValuePair unit.name { file = unit.text; mode = "0644"; } ) config.systemd.units; units-aliases = mkMerge ( mapAttrsToList ( _: unit: mkMerge ( map (aka: { ${aka} = symlink "${unit.name}"; }) (unit.aliases or [ ]) ) ) config.systemd.units ); units-extraWants = mkMerge ( mapAttrsToList ( _: unit: mkMerge ( map (unit2: { "${unit2}.wants" = dir { ${unit.name} = symlink "../${unit.name}"; }; }) (unit.wantedBy or [ ]) ) ) config.systemd.units ); units-extraUpholds = mkMerge ( mapAttrsToList ( _: unit: mkMerge ( map (unit2: { "${unit2}.upholds" = dir { ${unit.name} = symlink "../${unit.name}"; }; }) (unit.upheldBy or [ ]) ) ) config.systemd.units ); units-extraRequires = mkMerge ( mapAttrsToList ( _: unit: mkMerge ( map (unit2: { "${unit2}.requires" = dir { ${unit.name} = symlink "../${unit.name}"; }; }) (unit.requiredBy or [ ]) ) ) config.systemd.units ); in { options = { systemd = { units = mkOption { type = systemd-types.units; }; services = mkOption { type = systemd-types.services; }; targets = mkOption { type = systemd-types.targets; }; }; }; config = { systemd = { units = mkMerge [ (mapAttrs' (_: service: nameValuePair service.name (serviceToUnit service)) config.systemd.services) (mapAttrs' (_: target: nameValuePair target.name (targetToUnit target)) config.systemd.targets) ]; services = { getty = { wantedBy = [ "default.target" ]; unitConfig = { Description = "Serial Shell"; Before = [ "default.target" ]; }; script = '' # . /etc/profile exec /bin/ash < /dev/ttyS0 > /dev/ttyS0 2> /dev/ttyS0 ''; }; }; targets = { default = { }; sysinit = { }; }; }; kernel.config = { CGROUPS = "y"; DEVTMPFS = "y"; INOTIFY_USER = "y"; SIGNALFD = "y"; TIMERFD = "y"; EPOLL = "y"; UNIX = "y"; SYSFS = "y"; PROC_FS = "y"; FHANDLE = "y"; }; boot.commandLine = [ "systemd.log_level=7" #"systemd.crash_shell=true" ]; filesystem = dir { etc = dir { systemd = dir { system = dir (mkMerge [ units-texts units-aliases units-extraWants units-extraUpholds units-extraRequires ]); }; }; bin = dir { init = symlink "${pkgs.systemd}/bin/init"; }; }; }; }