From 109c57708461d74a14dc372e7e40a2d52566aef4 Mon Sep 17 00:00:00 2001 From: catvayor Date: Mon, 30 Sep 2024 16:39:58 +0200 Subject: [PATCH 1/8] systemd override --- default.nix | 2 +- modules/s6/default.nix | 5 ++- overlay.nix | 97 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/default.nix b/default.nix index fd1c357..a456110 100644 --- a/default.nix +++ b/default.nix @@ -57,7 +57,7 @@ let ]; }).config.system; in { - inherit evalModules; + inherit evalModules config; outputs = config.system.outputs // { default = config.system.outputs.${config.hardware.defaultOutput}; diff --git a/modules/s6/default.nix b/modules/s6/default.nix index e1ce9c8..376d4ce 100644 --- a/modules/s6/default.nix +++ b/modules/s6/default.nix @@ -186,8 +186,11 @@ in { }; }; }; + # bin = dir { + # init = symlink "${s6-init-bin}/bin/init"; + # }; bin = dir { - init = symlink "${s6-init-bin}/bin/init"; + init = symlink "${pkgs.systemd}/bin/init"; }; }; }; diff --git a/overlay.nix b/overlay.nix index 891e9a0..be76616 100644 --- a/overlay.nix +++ b/overlay.nix @@ -259,7 +259,7 @@ extraPkgs // { patches = o.patches ++ [ ./pkgs/qemu/arm-image-friendly-load-addr.patch ]; - }); in q.override { nixosTestRunner = true; sdlSupport = false; }; + }); in q.override { nixosTestRunner = true; sdlSupport = false; hostCpuTargets = [ "mips-softmmu" ]; }; rsyncSmall = let r = prev.rsync.overrideAttrs(o: { @@ -284,6 +284,101 @@ extraPkgs // { strace = prev.strace.override { libunwind = null; }; + getent = + prev.callPackage ({ + lib, + stdenv, + fetchFromGitHub, + }: + + stdenv.mkDerivation rec { + pname = "musl-utils"; + version = "1.1.12-r7"; + + src = fetchFromGitHub { + owner = "boltlinux"; + repo = "musl-utils"; + rev = version; + hash = "sha256-q9CbOyK0Psw3YJnIT3yPRWuwr40nxLZt4RexuOHAUuw="; + }; + + nativeBuildInputs = [ + prev.autoconf + prev.automake + ]; + + preConfigure = '' + autoreconf -i + ''; + + buildPhase = '' + make -C src getent + ''; + + installPhase = '' + mkdir -p $out/bin/ + cp src/getent $out/bin/ + ''; + + meta = { + description = "Alpine Linux' getconf, getent and iconv implementations"; + homepage = "https://github.com/boltlinux/musl-utils"; + license = lib.licenses.free; # FIXME: nix-init did not find a license + maintainers = with lib.maintainers; [ ]; + mainProgram = "musl-utils"; + platforms = lib.platforms.all; + }; + }) { }; + + systemd = prev.systemd.override { + withAcl = false; + withAnalyze = false; + withApparmor = false; + withAudit = false; + withBootloader = false; + # withCompression = true; + withCoredump = false; + withCryptsetup = false; + withRepart = false; + withDocumentation = false; + withEfi = false; + withFido2 = false; + withHomed = false; + # withHostnamed = true; + withHwdb = false; + withImportd = false; + withIptables = false; + withKmod = false; + withLibBPF = false; + withLibidn2 = false; + withLocaled = false; + # withLogind = true; + withMachined = false; + # withNetworkd = true; + withNss = false; + withOomd = false; + # withPam = true; + # withPasswordQuality = true; + withPCRE2 = false; + withPolkit = false; + withPortabled = false; + withQrencode = false; + withRemote = false; + # withResolved = true; + # withShellCompletions = true; + # withSysusers = true; + withSysupdate = false; + # withTimedated = true; + # withTimesyncd = true; + withTpm2Tss = false; + withUkify = false; + withUserDb = false; + withUtmp = false; + withVmspawn = false; + withKernelInstall = false; + withLibarchive = false; + }; + ubootQemuAarch64 = final.buildUBoot { defconfig = "qemu_arm64_defconfig"; extraMeta.platforms = ["aarch64-linux"]; -- 2.47.0 From ae78f9aafd872af9ff53c1fb040b8e331faa69fd Mon Sep 17 00:00:00 2001 From: catvayor Date: Mon, 30 Sep 2024 19:38:13 +0200 Subject: [PATCH 2/8] systemd init --- lib/eval-config.nix | 2 +- modules/base.nix | 3 +-- modules/s6/scripts/rc.shutdown | 19 ------------------- modules/s6/scripts/rc.shutdown.final | 22 ---------------------- modules/s6/scripts/runlevel | 12 ------------ modules/systemd/default.nix | 25 +++++++++++++++++++++++++ overlay.nix | 10 +++++----- 7 files changed, 32 insertions(+), 61 deletions(-) delete mode 100755 modules/s6/scripts/rc.shutdown delete mode 100755 modules/s6/scripts/rc.shutdown.final delete mode 100755 modules/s6/scripts/runlevel create mode 100644 modules/systemd/default.nix diff --git a/lib/eval-config.nix b/lib/eval-config.nix index 4c3c009..986e9d5 100644 --- a/lib/eval-config.nix +++ b/lib/eval-config.nix @@ -14,7 +14,7 @@ in "${modulesPath}/busybox.nix" "${modulesPath}/hostname.nix" "${modulesPath}/kernel" - "${modulesPath}/s6" + "${modulesPath}/systemd" "${modulesPath}/users.nix" "${modulesPath}/outputs.nix" "${modulesPath}/nixpkgs.nix" diff --git a/modules/base.nix b/modules/base.nix index 5107e1e..30141a8 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -129,10 +129,9 @@ in { # By default, we enable cross-compilation support. nixpkgs.buildPlatform = lib.mkDefault builtins.currentSystem; - defaultProfile.packages = with pkgs; - [ s6 s6-init-bin execline s6-linux-init s6-rc ]; # Set the useful PS1 prompt by default. defaultProfile.environmentVariables.PS1 = lib.mkDefault config.defaultProfile.prompt; + defaultProfile.packages = with pkgs; [ execline ]; boot.commandLine = [ "panic=10 oops=panic init=/bin/init loglevel=8" diff --git a/modules/s6/scripts/rc.shutdown b/modules/s6/scripts/rc.shutdown deleted file mode 100755 index 81fac67..0000000 --- a/modules/s6/scripts/rc.shutdown +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - - -### Things to do before hardware halt/reboot/poweroff. -### Ideally, it should be a single call to the service manager, -### telling it to bring all the services down. - -### If your s6-linux-init-maker invocation was made with the -1 -### option, messages from rc.shutdown will appear on /dev/console -### as well as be logged by the catch-all logger. -### If your s6-linux-init-maker invocation did NOT include the -1 -### option, messages from rc.shutdown will only be logged by the -### catch-all logger and will NOT appear on /dev/console. In order -### to print them to /dev/console instead, you may want to -### uncomment the following line: -exec >/dev/console 2>&1 - -### If your services are managed by s6-rc: -exec s6-rc -v2 -bDa change diff --git a/modules/s6/scripts/rc.shutdown.final b/modules/s6/scripts/rc.shutdown.final deleted file mode 100755 index 5344b7a..0000000 --- a/modules/s6/scripts/rc.shutdown.final +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh -e - -## s6-linux-init-shutdownd never tells s6-svscan to exit, so if -## you're running s6-linux-init, it's normal that your -## .s6-svscan/finish script is not executed. - -## The place where you want to hack things is /etc/rc.shutdown.final, -## which is run by the stage 4 script right before the hard reboot. -## So you can do dirty stuff [...] which should clean up the -## s6-supervise and the foreground, and give control to -## .s6-svscan/finish. - -## -- Laurent Bercot on skaware mailing list, -## https://skarnet.org/lists/skaware/1913.html - -exec >/dev/console 2>&1 - -# down, exit supervisor, wait, stay down -s6-svc -dxwD /run/service/s6-linux-init-shutdownd -# HUP, exit supervisor, wait, down -s6-svc -hxwd /run/service/s6-svscan-log -s6-svscanctl -b /run/service # abort diff --git a/modules/s6/scripts/runlevel b/modules/s6/scripts/runlevel deleted file mode 100755 index 3c2cfcd..0000000 --- a/modules/s6/scripts/runlevel +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/sh -e - -### This script is called once at boot time by rc.init, and is -### also called by the runleveld service every time the user -### requests a machine state change via telinit. -### Ideally, it should just be a call to the service manager. - -test "$#" -gt 0 || { echo 'runlevel: fatal: too few arguments' 1>&2 ; exit 100 ; } - - -### If your services are managed by s6-rc: -exec s6-rc -v2 -up change "$1" diff --git a/modules/systemd/default.nix b/modules/systemd/default.nix new file mode 100644 index 0000000..7f6c1dc --- /dev/null +++ b/modules/systemd/default.nix @@ -0,0 +1,25 @@ +{ pkgs, ... }: +let + inherit (pkgs.pseudofile) dir symlink; +in +{ + config = { + boot.commandLine = [ + "systemd.log_level=7" + #"systemd.crash_shell=true" + ]; + filesystem = dir { + etc = dir { + systemd = dir { + "system.conf" = { + file = ''''; + mode = "0444"; + }; + }; + }; + bin = dir { + init = symlink "${pkgs.systemd}/bin/init"; + }; + }; + }; +} diff --git a/overlay.nix b/overlay.nix index be76616..74327e2 100644 --- a/overlay.nix +++ b/overlay.nix @@ -352,19 +352,19 @@ extraPkgs // { withLibBPF = false; withLibidn2 = false; withLocaled = false; - # withLogind = true; + withLogind = false; withMachined = false; - # withNetworkd = true; + withNetworkd = false; withNss = false; withOomd = false; - # withPam = true; - # withPasswordQuality = true; + withPam = false; + withPasswordQuality = false; withPCRE2 = false; withPolkit = false; withPortabled = false; withQrencode = false; withRemote = false; - # withResolved = true; + withResolved = false; # withShellCompletions = true; # withSysusers = true; withSysupdate = false; -- 2.47.0 From 8c97f3e2571bb6952cb88771ca14f47b6ba15e10 Mon Sep 17 00:00:00 2001 From: catvayor Date: Tue, 1 Oct 2024 11:00:15 +0200 Subject: [PATCH 3/8] agetty --- modules/systemd/default.nix | 49 ++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/modules/systemd/default.nix b/modules/systemd/default.nix index 7f6c1dc..7b28730 100644 --- a/modules/systemd/default.nix +++ b/modules/systemd/default.nix @@ -1,9 +1,53 @@ { pkgs, ... }: let inherit (pkgs.pseudofile) dir symlink; + "getty.service" = { + file = + let + login = pkgs.writeScript "login" '' + #!/bin/ash + exec /bin/ash + ''; + in + '' + [Unit] + Description="Serial shell" + + [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 = { + 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" @@ -11,9 +55,8 @@ in filesystem = dir { etc = dir { systemd = dir { - "system.conf" = { - file = ''''; - mode = "0444"; + system = dir { + inherit "default.target" "getty.service" "sysinit.target"; }; }; }; -- 2.47.0 From 14b59b5b6209ed195e496f558781d46a9f1a9142 Mon Sep 17 00:00:00 2001 From: catvayor Date: Tue, 1 Oct 2024 16:43:53 +0200 Subject: [PATCH 4/8] begin minimization of systemd trivial part --- overlay.nix | 160 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 111 insertions(+), 49 deletions(-) diff --git a/overlay.nix b/overlay.nix index 74327e2..1e9b150 100644 --- a/overlay.nix +++ b/overlay.nix @@ -259,7 +259,12 @@ extraPkgs // { patches = o.patches ++ [ ./pkgs/qemu/arm-image-friendly-load-addr.patch ]; - }); in q.override { nixosTestRunner = true; sdlSupport = false; hostCpuTargets = [ "mips-softmmu" ]; }; + }); in q.override { + vde2 = null; + nixosTestRunner = true; + sdlSupport = false; + hostCpuTargets = [ "mips-softmmu" ]; + }; rsyncSmall = let r = prev.rsync.overrideAttrs(o: { @@ -330,54 +335,111 @@ extraPkgs // { }; }) { }; - systemd = prev.systemd.override { - withAcl = false; - withAnalyze = false; - withApparmor = false; - withAudit = false; - withBootloader = false; - # withCompression = true; - withCoredump = false; - withCryptsetup = false; - withRepart = false; - withDocumentation = false; - withEfi = false; - withFido2 = false; - withHomed = false; - # withHostnamed = true; - withHwdb = false; - withImportd = false; - withIptables = false; - withKmod = false; - withLibBPF = false; - withLibidn2 = false; - withLocaled = false; - withLogind = false; - withMachined = false; - withNetworkd = false; - withNss = false; - withOomd = false; - withPam = false; - withPasswordQuality = false; - withPCRE2 = false; - withPolkit = false; - withPortabled = false; - withQrencode = false; - withRemote = false; - withResolved = false; - # withShellCompletions = true; - # withSysusers = true; - withSysupdate = false; - # withTimedated = true; - # withTimesyncd = true; - withTpm2Tss = false; - withUkify = false; - withUserDb = false; - withUtmp = false; - withVmspawn = false; - withKernelInstall = false; - withLibarchive = false; - }; + systemd = + let base = prev.systemd.override { + withAcl = false; + withAnalyze = false; + withApparmor = false; + withAudit = false; + withBootloader = false; + withCompression = false; + withCoredump = false; + withCryptsetup = false; + withRepart = false; + withDocumentation = false; + withEfi = false; + withFido2 = false; + withHomed = false; + withHostnamed = false; + withHwdb = false; + withImportd = false; + withIptables = false; + withKmod = false; + withLibBPF = false; + withLibidn2 = false; + withLocaled = false; + withLogind = false; + withMachined = false; + withNetworkd = false; + withNss = false; + withOomd = false; + withPam = false; + withPasswordQuality = false; + withPCRE2 = false; + withPolkit = false; + withPortabled = false; + withQrencode = false; + withRemote = false; + withResolved = false; + withShellCompletions = false; + withSysusers = false; + withSysupdate = false; + withTimedated = false; + withTimesyncd = false; + withTpm2Tss = false; + withUkify = false; + withUserDb = false; + withUtmp = false; + withVmspawn = false; + withKernelInstall = false; + withLibarchive = false; + }; + in base.overrideAttrs (o: { + mesonFlags = o.mesonFlags ++ [ + # "--optimization=s" + "--default-library=static" + "--buildtype=minsize" + ]; + postInstall = o.postInstall + '' + rm -rf $out/share + rm $out/lib/libudev.so* + rm -rf $out/lib/systemd/catalog + rm -rf $out/lib/systemd/system-generators + rm $out/lib/systemd/systemd-backlight + rm $out/lib/systemd/systemd-battery-check + rm $out/lib/systemd/systemd-hibernate-resume + rm $out/lib/systemd/systemd-makefs + rm $out/lib/systemd/systemd-nsresourced + rm $out/lib/systemd/systemd-nsresourcework + rm $out/lib/systemd/systemd-shutdown + rm $out/lib/systemd/systemd-sleep + rm $out/lib/systemd/systemd-binfmt + rm $out/lib/systemd/systemd-growfs + rm $out/lib/systemd/systemd-mountfsd + rm $out/lib/systemd/systemd-mountwork + rm $out/lib/systemd/systemd-network-generator + rm $out/lib/systemd/systemd-pstore + rm $out/lib/systemd/systemd-remount-fs + rm $out/lib/systemd/systemd-reply-password + rm $out/lib/systemd/systemd-rfkill + rm $out/lib/systemd/systemd-socket-proxyd + rm $out/lib/systemd/systemd-ssh-proxy + rm $out/lib/systemd/systemd-storagetm + rm $out/lib/systemd/systemd-volatile-root + rm $out/lib/systemd/systemd-xdg-autostart-condition + rm -rf $out/example + rm $out/bin/bootctl + rm $out/bin/systemd-nspawn + rm $out/bin/systemd-ac-power + rm $out/bin/systemd-dissect + rm $out/bin/systemd-ask-password + rm $out/bin/systemd-cgls + rm $out/bin/systemd-cgtop + rm $out/bin/systemd-creds + rm $out/bin/systemd-delta + rm $out/bin/systemd-detect-virt + rm $out/bin/systemd-escape + rm $out/bin/systemd-id128 + rm $out/bin/systemd-machine-id-setup + rm $out/bin/systemd-path + rm $out/bin/systemd-run + rm $out/bin/systemd-socket-activate + rm $out/bin/systemd-stdio-bridge + rm $out/bin/systemd-sysext + rm $out/bin/systemd-tty-ask-password-agent + rm $out/bin/systemd-vpick + ''; + }); ubootQemuAarch64 = final.buildUBoot { defconfig = "qemu_arm64_defconfig"; -- 2.47.0 From 7705bc2a8ec528e5f06341e5143c03f9c7f89099 Mon Sep 17 00:00:00 2001 From: catvayor Date: Sat, 5 Oct 2024 11:50:12 +0200 Subject: [PATCH 5/8] units --- default.nix | 10 ++- modules/base.nix | 8 +- modules/systemd/default.nix | 153 +++++++++++++++++++++++++++--------- 3 files changed, 133 insertions(+), 38 deletions(-) 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 { -- 2.47.0 From 9da241d8eca045d597f1e8e220db04cdfc68b497 Mon Sep 17 00:00:00 2001 From: catvayor Date: Sun, 6 Oct 2024 18:38:36 +0200 Subject: [PATCH 6/8] services & targets --- modules/base.nix | 3 -- modules/systemd/default.nix | 84 ++++++++++++++++++------------------- modules/systemd/types.nix | 27 ++++++++++++ 3 files changed, 69 insertions(+), 45 deletions(-) create mode 100644 modules/systemd/types.nix diff --git a/modules/base.nix b/modules/base.nix index 257200e..1755f83 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -49,9 +49,6 @@ in { 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 17cf0cc..8a9a15c 100644 --- a/modules/systemd/default.nix +++ b/modules/systemd/default.nix @@ -11,6 +11,7 @@ let nameValuePair mkMerge mapAttrsToList + mkOption ; inherit (pkgs.pseudofile) dir symlink; inherit (utils.systemdUtils.lib) @@ -18,13 +19,15 @@ let serviceToUnit ; + systemd-types = import ./types.nix { inherit pkgs utils lib; }; + units-texts = mapAttrs' ( _: unit: nameValuePair unit.name { file = unit.text; mode = "0644"; } - ) config.units; + ) config.systemd.units; units-aliases = mkMerge ( mapAttrsToList ( _: unit: @@ -33,7 +36,7 @@ let ${aka} = symlink "${unit.name}"; }) (unit.aliases or [ ]) ) - ) config.units + ) config.systemd.units ); units-extraWants = mkMerge ( mapAttrsToList ( @@ -45,7 +48,7 @@ let }; }) (unit.wantedBy or [ ]) ) - ) config.units + ) config.systemd.units ); units-extraUpholds = mkMerge ( mapAttrsToList ( @@ -57,7 +60,7 @@ let }; }) (unit.upheldBy or [ ]) ) - ) config.units + ) config.systemd.units ); units-extraRequires = mkMerge ( mapAttrsToList ( @@ -69,49 +72,46 @@ let }; }) (unit.requiredBy or [ ]) ) - ) config.units + ) 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 = { - 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" ]; + 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 = '' + #!/bin/ash + # . /etc/profile + exec /bin/ash < /dev/ttyS0 > /dev/ttyS0 2> /dev/ttyS0 + ''; }; - serviceConfig.ExecStart = - let - login = pkgs.writeScript "login" '' - #!/bin/ash - . /etc/profile - exec /bin/ash - ''; - in - "${pkgs.util-linux}/bin/agetty --login-program ${login} ttyS0"; + }; + targets = { + default = { }; + sysinit = { }; }; }; diff --git a/modules/systemd/types.nix b/modules/systemd/types.nix new file mode 100644 index 0000000..239f551 --- /dev/null +++ b/modules/systemd/types.nix @@ -0,0 +1,27 @@ +{ + pkgs, + lib, + utils +}: +let + inherit (utils.systemdUtils.lib) serviceConfig unitConfig; + inherit (utils.systemdUtils.unitOptions) stage2ServiceOptions; + stage2ServiceConfig = { + imports = [ serviceConfig ]; + # Default path for systemd services. Should be quite minimal. + config.path = lib.mkAfter [ + pkgs.coreutils + # pkgs.gnugrep + # pkgs.gnused + pkgs.systemd + ]; + }; +in +{ + inherit (utils.systemdUtils.types) units targets; + services = lib.types.attrsOf (lib.types.submodule [ + unitConfig + stage2ServiceOptions + stage2ServiceConfig + ]); +} -- 2.47.0 From 7219e6699289e8a2bf68e31f8af2406f88edd010 Mon Sep 17 00:00:00 2001 From: catvayor Date: Sun, 6 Oct 2024 23:27:48 +0200 Subject: [PATCH 7/8] reduce closure --- modules/base.nix | 5 ++++- modules/busybox.nix | 12 +++++++++--- modules/systemd/default.nix | 3 +-- modules/systemd/types.nix | 6 ++++-- overlay.nix | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 8 deletions(-) diff --git a/modules/base.nix b/modules/base.nix index 1755f83..a12cd6e 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -134,7 +134,10 @@ in { # Set the useful PS1 prompt by default. defaultProfile.environmentVariables.PS1 = lib.mkDefault config.defaultProfile.prompt; - defaultProfile.packages = with pkgs; [ execline ]; + defaultProfile.packages = with pkgs; [ + # execline + systemd + ]; boot.commandLine = [ "panic=10 oops=panic init=/bin/init loglevel=8" diff --git a/modules/busybox.nix b/modules/busybox.nix index 718cafd..d22a8c3 100644 --- a/modules/busybox.nix +++ b/modules/busybox.nix @@ -51,7 +51,7 @@ let in { options = { programs.busybox = { - applets = mkOption { + applets = mkOption { type = types.listOf types.str; description = "Applets required"; default = []; @@ -62,13 +62,19 @@ in { # other strings are also used description = "Other busybox config flags that do not map directly to applet names (often prefixed FEATURE_)"; type = types.attrsOf types.nonEmptyStr; - default = { }; - example = { FEATURE_DD_IBS_OBS = "y"; }; + default = { }; + example = { FEATURE_DD_IBS_OBS = "y"; }; + }; + package = mkOption { + type = types.package; + readOnly = true; + internal = true; }; }; }; config = { programs.busybox = { + package = busybox; applets = minimalApplets; options = { ASH_ECHO = "y"; diff --git a/modules/systemd/default.nix b/modules/systemd/default.nix index 8a9a15c..6dfdc3a 100644 --- a/modules/systemd/default.nix +++ b/modules/systemd/default.nix @@ -19,7 +19,7 @@ let serviceToUnit ; - systemd-types = import ./types.nix { inherit pkgs utils lib; }; + systemd-types = import ./types.nix { inherit pkgs utils lib; busybox = config.programs.busybox.package; }; units-texts = mapAttrs' ( _: unit: @@ -103,7 +103,6 @@ in Before = [ "default.target" ]; }; script = '' - #!/bin/ash # . /etc/profile exec /bin/ash < /dev/ttyS0 > /dev/ttyS0 2> /dev/ttyS0 ''; diff --git a/modules/systemd/types.nix b/modules/systemd/types.nix index 239f551..87c974e 100644 --- a/modules/systemd/types.nix +++ b/modules/systemd/types.nix @@ -1,7 +1,8 @@ { pkgs, lib, - utils + utils, + busybox }: let inherit (utils.systemdUtils.lib) serviceConfig unitConfig; @@ -10,7 +11,8 @@ let imports = [ serviceConfig ]; # Default path for systemd services. Should be quite minimal. config.path = lib.mkAfter [ - pkgs.coreutils + busybox + # pkgs.coreutils # pkgs.gnugrep # pkgs.gnused pkgs.systemd diff --git a/overlay.nix b/overlay.nix index 1e9b150..c79866c 100644 --- a/overlay.nix +++ b/overlay.nix @@ -335,8 +335,28 @@ extraPkgs // { }; }) { }; + writeShellScriptBin = name: text: + final.writeTextFile { + inherit name; + executable = true; + destination = "/bin/${name}"; + text = '' + #!/bin/ash + ${text} + ''; + checkPhase = '' + ${final.stdenv.shellDryRun} "$target" + ''; + meta.mainProgram = name; + }; + systemd = let base = prev.systemd.override { + kbd = ""; + coreutils = ""; + bash = null; + bashInteractive = ""; + withAcl = false; withAnalyze = false; withApparmor = false; @@ -389,6 +409,17 @@ extraPkgs // { # "--optimization=s" "--default-library=static" "--buildtype=minsize" + # (final.lib.mesonBool "static-libsystemd" true) + # (final.lib.mesonBool "standalone-binaries" true) + + # (final.lib.mesonBool "link-udev-shared" false) + # (final.lib.mesonBool "link-executor-shared" false) + # (final.lib.mesonBool "link-systemctl-shared" false) + # (final.lib.mesonBool "link-networkd-shared" false) + # (final.lib.mesonBool "link-timesyncd-shared" false) + # (final.lib.mesonBool "link-journalctl-shared" false) + # (final.lib.mesonBool "link-boot-shared" false) + # (final.lib.mesonBool "link-portabled-shared" false) ]; postInstall = o.postInstall + '' rm -rf $out/share @@ -438,6 +469,9 @@ extraPkgs // { rm $out/bin/systemd-sysext rm $out/bin/systemd-tty-ask-password-agent rm $out/bin/systemd-vpick + + # rm $out/lib/libsystemd.a + # rm $out/lib/systemd/libsystemd-shared-256.so ''; }); -- 2.47.0 From 643264bd69660dd01df51d72e00be56cb9fef13d Mon Sep 17 00:00:00 2001 From: catvayor Date: Fri, 8 Nov 2024 22:39:23 +0100 Subject: [PATCH 8/8] fix(update): enableScrictShellChecks --- modules/systemd/types.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/systemd/types.nix b/modules/systemd/types.nix index 87c974e..30ca1fe 100644 --- a/modules/systemd/types.nix +++ b/modules/systemd/types.nix @@ -22,6 +22,7 @@ in { inherit (utils.systemdUtils.types) units targets; services = lib.types.attrsOf (lib.types.submodule [ + { enableStrictShellChecks = false; } unitConfig stage2ServiceOptions stage2ServiceConfig -- 2.47.0