From 65de89601977c8c02af843982e7b70edd1913a74 Mon Sep 17 00:00:00 2001 From: catvayor Date: Tue, 27 Aug 2024 10:57:31 +0200 Subject: [PATCH 01/57] feat(ci): build VM QEMU MIPS --- .forgejo/workflows/build.yaml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .forgejo/workflows/build.yaml diff --git a/.forgejo/workflows/build.yaml b/.forgejo/workflows/build.yaml new file mode 100644 index 0000000..dcb2ee4 --- /dev/null +++ b/.forgejo/workflows/build.yaml @@ -0,0 +1,20 @@ +name: build liminix +on: + pull_request: + types: [opened, synchronize, edited, reopened] + branches: + - main + push: + branches: + - main + +jobs: + build_vm_qemu_mips: + runs-on: nix + steps: + - uses: actions/checkout@v3 + + - name: Build VM QEMU MIPS + run: | + # Enter the shell + nix-build -I liminix-config=./examples/hello-from-qemu.nix --arg device "import ./devices/qemu" -A outputs.default -- 2.47.0 From 2d6414ea41811adead271b60483e89f338261362 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 26 Aug 2024 17:58:40 +0200 Subject: [PATCH 02/57] tftp: introduce the FIT enclosing boot.scr This simplify TFTP. Signed-off-by: Raito Bezarius --- modules/outputs/tftpboot-fit.its | 14 +++++++++++++ modules/outputs/tftpboot.nix | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 modules/outputs/tftpboot-fit.its diff --git a/modules/outputs/tftpboot-fit.its b/modules/outputs/tftpboot-fit.its new file mode 100644 index 0000000..fcb58ab --- /dev/null +++ b/modules/outputs/tftpboot-fit.its @@ -0,0 +1,14 @@ +/dts-v1/; + +/ { + description = "Liminix TFTP bootscript"; + #address-cells = <1>; + + images { + bootscript { + description = "Bootscript"; + data = /incbin/("boot.scr"); + type = "script"; + compression = "none"; + }; +}; diff --git a/modules/outputs/tftpboot.nix b/modules/outputs/tftpboot.nix index f8f0572..94d5ef4 100644 --- a/modules/outputs/tftpboot.nix +++ b/modules/outputs/tftpboot.nix @@ -51,11 +51,47 @@ in { It uses the Linux `phram `_ driver to emulate a flash device using a segment of physical RAM. ''; }; + + tftpboot-fit = mkOption { + type = types.package; + description = '' + tftpboot-fit + ************ + + This output is a variant that encloses the `boot.scr` in a FIT + if that's simpler to transfer for you. + ''; + }; }; config = { boot.ramdisk.enable = true; system.outputs = rec { + tftpboot-fit = + let + tftpboot-fit = pkgs.writeText "tftpboot.its" '' + /dts-v1/; + + / { + description = "Liminix TFTP bootscript"; + #address-cells = <1>; + + images { + bootscript { + description = "Bootscript"; + data = /incbin/("${tftpboot}/boot.scr"); + type = "script"; + compression = "none"; + }; + }; + }; + ''; + in + pkgs.runCommand "tftpboot-fit" { nativeBuildInputs = with pkgs.pkgsBuildBuild; [ ubootTools ]; } '' + mkdir -p $out/ + cp -rf ${tftpboot}/* $out/ + mkimage -f ${tftpboot-fit} $out/script.ub + ''; tftpboot = let inherit (pkgs.lib.trivial) toHexString; -- 2.47.0 From 86e81efbd6657a1f977d276d5926582e8e619395 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 26 Aug 2024 17:53:08 +0200 Subject: [PATCH 03/57] tftp: introduce an alternative command line for TFTP Normal command line and TFTP command line can be sometimes very different. e.g. We don't want to load UBI filesystems for a TFTP boot as it may interfere with our root device loading. Signed-off-by: Raito Bezarius --- modules/arch/mips.nix | 3 +++ modules/base.nix | 6 ++++++ modules/outputs/tftpboot.nix | 10 +++++++++- modules/outputs/ubivolume.nix | 7 +++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/arch/mips.nix b/modules/arch/mips.nix index 155c147..359bb71 100644 --- a/modules/arch/mips.nix +++ b/modules/arch/mips.nix @@ -14,5 +14,8 @@ boot.commandLine = [ "console=ttyS0,115200" # true of all mips we've yet encountered ]; + boot.tftp.commandLine = [ + "console=ttyS0,115200" # true of all mips we've yet encountered + ]; }; } diff --git a/modules/base.nix b/modules/base.nix index 1c8b67c..4c179ec 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -111,6 +111,12 @@ in { ++ lib.optional (config.rootOptions != null) "rootflags=${config.rootOptions}" ++ lib.optional (config.hardware.alternativeRootDevice != null) "altroot=${config.hardware.alternativeRootDevice}"; + boot.tftp.commandLine = [ + "panic=10 oops=panic init=/bin/init loglevel=8" + "fw_devlink=off" + "rootfstype=${config.rootfsType}" + ]; + system.callService = path : parameters : let typeChecked = caller: type: value: diff --git a/modules/outputs/tftpboot.nix b/modules/outputs/tftpboot.nix index 94d5ef4..6d5bad9 100644 --- a/modules/outputs/tftpboot.nix +++ b/modules/outputs/tftpboot.nix @@ -28,6 +28,14 @@ in { type = types.bool; default = false; }; + commandLine = mkOption { + type = types.listOf types.str; + default = config.boot.commandLine; + description = '' + TFTP-specific command line. + Defaults to the classical one if unset. + ''; + }; }; options.system.outputs = { tftpboot = mkOption { @@ -105,7 +113,7 @@ in { zimage = "bootz"; }; in choices.${cfg.kernelFormat}; - cmdline = concatStringsSep " " config.boot.commandLine; + cmdline = concatStringsSep " " config.boot.tftp.commandLine; objcopy = "${pkgs.stdenv.cc.bintools.targetPrefix}objcopy"; stripAndZip = '' ${objcopy} -O binary -R .reginfo -R .notes -R .note -R .comment -R .mdebug -R .note.gnu.build-id -S vmlinux.elf vmlinux.bin diff --git a/modules/outputs/ubivolume.nix b/modules/outputs/ubivolume.nix index 45aa889..1b9decc 100644 --- a/modules/outputs/ubivolume.nix +++ b/modules/outputs/ubivolume.nix @@ -29,6 +29,13 @@ in }; boot.initramfs.enable = true; + # In TFTP, the device named "rootfs" is the UBI device. + # We tell the kernel to load it. + # This avoids interference from the other UBI volumes. + boot.tftp.commandLine = [ + "ubi.mtd=rootfs" + ]; + system.outputs.rootfs = let inherit (pkgs.pkgsBuildBuild) runCommand; -- 2.47.0 From 8f5ea94765fb8449ca72cefa2bb1fa2e17a03fb6 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 26 Aug 2024 18:24:44 +0200 Subject: [PATCH 04/57] chore(zyxel/nwa50ax): write flash erase block size as kb size Signed-off-by: Raito Bezarius --- devices/zyxel-nwa50ax/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devices/zyxel-nwa50ax/default.nix b/devices/zyxel-nwa50ax/default.nix index e58430f..73f9214 100644 --- a/devices/zyxel-nwa50ax/default.nix +++ b/devices/zyxel-nwa50ax/default.nix @@ -170,7 +170,7 @@ maxLEBcount = "256"; }; - flash.eraseBlockSize = 65536; + flash.eraseBlockSize = 64 * 1024; # This is a FIT containing a kernel padded and # a UBI volume rootfs. -- 2.47.0 From ea740bbfafd96448f1161e1807763b932e830fbd Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 26 Aug 2024 18:32:46 +0200 Subject: [PATCH 05/57] fix(zyxel/nwa50ax): make `altroot` useful Let's use `ubi1` if it exist, as it should be the second device containing a rootfs. Signed-off-by: Raito Bezarius --- devices/zyxel-nwa50ax/default.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/devices/zyxel-nwa50ax/default.nix b/devices/zyxel-nwa50ax/default.nix index 73f9214..e53e28f 100644 --- a/devices/zyxel-nwa50ax/default.nix +++ b/devices/zyxel-nwa50ax/default.nix @@ -181,8 +181,8 @@ # Aligned on 2kb. alignment = 2048; - rootDevice = "ubi:rootfs"; - alternativeRootDevice = "ubi:rootfs"; + rootDevice = "ubi0:rootfs"; + alternativeRootDevice = "ubi1:rootfs"; # Auto-attach MTD devices: ubi_a then ubi_b. ubi.mtds = [ "ubi_a" "ubi_b" ]; -- 2.47.0 From 85bfe94429c4c3798e10e5513e75fccacb120460 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 26 Aug 2024 18:38:01 +0200 Subject: [PATCH 06/57] fix(zyxel/nwa50ax): ensure the DTB is in the FIT Signed-off-by: Raito Bezarius --- devices/zyxel-nwa50ax/default.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/devices/zyxel-nwa50ax/default.nix b/devices/zyxel-nwa50ax/default.nix index e53e28f..393dcac 100644 --- a/devices/zyxel-nwa50ax/default.nix +++ b/devices/zyxel-nwa50ax/default.nix @@ -224,6 +224,7 @@ tftp = { # 5MB is nice. freeSpaceBytes = 5 * 1024 * 1024; + appendDTB = true; loadAddress = lim.parseInt "0x2000000"; }; }; -- 2.47.0 From c1e61d6af5ee0b8529d0b9f77089bd34095f5277 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 26 Aug 2024 18:55:58 +0200 Subject: [PATCH 07/57] fix(zyxel/nwa50ax): ubi cannot run on phram Discovered the hard way. Signed-off-by: Raito Bezarius --- modules/outputs/jffs2.nix | 18 ++++++------------ modules/outputs/tftpboot.nix | 23 ++++++++++++++++++----- modules/outputs/ubivolume.nix | 7 ------- pkgs/default.nix | 1 + pkgs/liminix-tools/builders/jffs2.nix | 17 +++++++++++++++++ 5 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 pkgs/liminix-tools/builders/jffs2.nix diff --git a/modules/outputs/jffs2.nix b/modules/outputs/jffs2.nix index 02c7c10..0a69a1f 100644 --- a/modules/outputs/jffs2.nix +++ b/modules/outputs/jffs2.nix @@ -5,7 +5,8 @@ , ... }: let - inherit (lib) mkIf mkOption types; + inherit (pkgs) liminix; + inherit (lib) mkIf; o = config.system.outputs; in { @@ -24,17 +25,10 @@ in }; boot.initramfs.enable = true; system.outputs = { - rootfs = - let - inherit (pkgs.pkgsBuildBuild) runCommand mtdutils; - endian = if pkgs.stdenv.isBigEndian - then "--big-endian" else "--little-endian"; - in runCommand "make-jffs2" { - depsBuildBuild = [ mtdutils ]; - } '' - tree=${o.bootablerootdir} - (cd $tree && mkfs.jffs2 --compression-mode=size ${endian} -e ${toString config.hardware.flash.eraseBlockSize} --enable-compressor=lzo --pad --root . --output $out --squash --faketime ) - ''; + rootfs = liminix.builders.jffs2 { + bootableRootDirectory = o.bootablerootdir; + inherit (config.hardware.flash) eraseBlockSize; + }; }; }; } diff --git a/modules/outputs/tftpboot.nix b/modules/outputs/tftpboot.nix index 6d5bad9..ca51d10 100644 --- a/modules/outputs/tftpboot.nix +++ b/modules/outputs/tftpboot.nix @@ -5,10 +5,18 @@ , ... }: let - inherit (lib) mkOption types concatStringsSep; + inherit (lib) mkOption mkIf types concatStringsSep; + inherit (pkgs) liminix; cfg = config.boot.tftp; hw = config.hardware; arch = pkgs.stdenv.hostPlatform.linuxArch; + + # UBI cannot run on the top of phram. + needsSquashfs = config.rootfsType == "ubifs"; + rootfstype = if needsSquashfs then "squashfs" else config.rootfsType; + rootfs = if needsSquashfs then + liminix.builders.squashfs config.filesystem.contents + else config.system.outputs.rootfs; in { imports = [ ../ramdisk.nix ]; options.boot.tftp = { @@ -74,6 +82,11 @@ in { config = { boot.ramdisk.enable = true; + kernel.config = mkIf needsSquashfs { + SQUASHFS = "y"; + SQUASHFS_XZ = "y"; + }; + system.outputs = rec { tftpboot-fit = let @@ -128,7 +141,7 @@ in { hex() { printf "0x%x" $1; } rootfsStart=${toString cfg.loadAddress} - rootfsSize=$(binsize64k ${o.rootfs} ) + rootfsSize=$(binsize64k ${rootfs} ) rootfsSize=$(($rootfsSize + ${toString cfg.freeSpaceBytes} )) ln -s ${o.manifest} manifest @@ -142,13 +155,13 @@ in { dtbStart=$(($rootfsStart + $rootfsSize)) ${if cfg.compressRoot then '' - lzma -z9cv ${o.rootfs} > rootfs.lz + lzma -z9cv ${rootfs} > rootfs.lz rootfsLzStart=$dtbStart rootfsLzSize=$(binsize rootfs.lz) dtbStart=$(($dtbStart + $rootfsLzSize)) '' else '' - ln -s ${o.rootfs} rootfs + ln -s ${rootfs} rootfs '' } @@ -165,7 +178,7 @@ in { fdtput -p -t s dtb /reserved-memory/$node compatible phram fdtput -p -t lx dtb /reserved-memory/$node reg $ac_prefix $(hex $rootfsStart) $sz_prefix $(hex $rootfsSize) - cmd="liminix ${cmdline} mtdparts=phram0:''${rootfsSize}(rootfs) phram.phram=phram0,''${rootfsStart},''${rootfsSize},${toString config.hardware.flash.eraseBlockSize} root=/dev/mtdblock0"; + cmd="liminix ${cmdline} mtdparts=phram0:''${rootfsSize}(rootfs) phram.phram=phram0,''${rootfsStart},''${rootfsSize},${toString config.hardware.flash.eraseBlockSize} rootfstype=${rootfstype} root=/dev/mtdblock0"; fdtput -t s dtb /chosen ${config.boot.commandLineDtbNode} "$cmd" dtbSize=$(binsize ./dtb ) diff --git a/modules/outputs/ubivolume.nix b/modules/outputs/ubivolume.nix index 1b9decc..45aa889 100644 --- a/modules/outputs/ubivolume.nix +++ b/modules/outputs/ubivolume.nix @@ -29,13 +29,6 @@ in }; boot.initramfs.enable = true; - # In TFTP, the device named "rootfs" is the UBI device. - # We tell the kernel to load it. - # This avoids interference from the other UBI volumes. - boot.tftp.commandLine = [ - "ubi.mtd=rootfs" - ]; - system.outputs.rootfs = let inherit (pkgs.pkgsBuildBuild) runCommand; diff --git a/pkgs/default.nix b/pkgs/default.nix index b63ab3a..7bb2470 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -13,6 +13,7 @@ in { liminix = { builders = { squashfs = callPackage ./liminix-tools/builders/squashfs.nix {}; + jffs2 = callPackage ./liminix-tools/builders/jffs2.nix {}; dtb = callPackage ./kernel/dtb.nix {}; uimage = callPackage ./kernel/uimage.nix {}; kernel = callPackage ./kernel {}; diff --git a/pkgs/liminix-tools/builders/jffs2.nix b/pkgs/liminix-tools/builders/jffs2.nix new file mode 100644 index 0000000..62378b4 --- /dev/null +++ b/pkgs/liminix-tools/builders/jffs2.nix @@ -0,0 +1,17 @@ +{ + stdenv +, busybox +, buildPackages +, callPackage +, pseudofile +, runCommand +, writeText +} : { eraseBlockSize, bootableRootDirectory }: +let + endian = if stdenv.isBigEndian then "--big-endian" else "--little-endian"; +in runCommand "frob-jffs2" { + depsBuildBuild = [ buildPackages.mtdutils ]; + } '' + tree=${bootableRootDirectory} + (cd $tree && mkfs.jffs2 --compression-mode=size ${endian} -e ${toString eraseBlockSize} --enable-compressor=lzo --pad --root . --output $out --squash --faketime) + '' -- 2.47.0 From 17355c3911836e76b4c113a74e70e681a4316c7a Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 26 Aug 2024 20:01:34 +0200 Subject: [PATCH 08/57] fix(preinit): scan multiple times the cmdline and rename altroot in rootalt The way the parsing works is examining one character at a time. First, if we had `rootfstype=... root=...`, the parsing would jump and ignore `root=...`, which sucks. To fix this, we scan multiple times a copy of the cmdline. Now, we have a new problem: `root=... altroot=...` lead to opts.device being equal to the altroot as we are looking one char at a time, so we will arrive at a moment looking at `root=...` for `altroot=...`. To avoid this, we rename `altroot` in `rootalt`, cheap, I know. Signed-off-by: Raito Bezarius --- modules/base.nix | 2 +- pkgs/preinit/parseopts.c | 36 ++++++++++++++++++++++++++---------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/modules/base.nix b/modules/base.nix index 4c179ec..d2182c6 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -109,7 +109,7 @@ in { ] ++ (map (mtd: "ubi.mtd=${mtd}") config.hardware.ubi.mtds) ++ lib.optional (config.rootOptions != null) "rootflags=${config.rootOptions}" - ++ lib.optional (config.hardware.alternativeRootDevice != null) "altroot=${config.hardware.alternativeRootDevice}"; + ++ lib.optional (config.hardware.alternativeRootDevice != null) "rootalt=${config.hardware.alternativeRootDevice}"; boot.tftp.commandLine = [ "panic=10 oops=panic init=/bin/init loglevel=8" diff --git a/pkgs/preinit/parseopts.c b/pkgs/preinit/parseopts.c index 1803341..243f3b0 100644 --- a/pkgs/preinit/parseopts.c +++ b/pkgs/preinit/parseopts.c @@ -65,13 +65,18 @@ static char * eat_param(char *p, char *param_name, char **out) return p; } +#define SCAN_CMDLINE(cmdline, identifier, field) do { \ + for (char* p = strdup(cmdline); *p; p++) { \ + p = eat_param(p, identifier, &(opts->field)); \ + } \ +} while(0) + + void parseopts(char * cmdline, struct root_opts *opts) { - for(char *p = cmdline; *p; p++) { - p = eat_param(p, "root=", &(opts->device)); - p = eat_param(p, "rootfstype=", &(opts->fstype)); - p = eat_param(p, "rootflags=", &(opts->mount_opts)); - p = eat_param(p, "altroot=", &(opts->altdevice)); - }; + SCAN_CMDLINE(cmdline, "root=", device); + SCAN_CMDLINE(cmdline, "rootfstype=", fstype); + SCAN_CMDLINE(cmdline, "rootflags=", mount_opts); + SCAN_CMDLINE(cmdline, "rootalt=", altdevice); } #ifdef TESTS @@ -85,6 +90,8 @@ void parseopts(char * cmdline, struct root_opts *opts) { #define S(x) #x #define expect_equal(actual, expected) \ if(!actual || strcmp(actual, expected)) die("%d: expected \"%s\", got \"%s\"", __LINE__, expected, actual); +#define expect_null(actual) \ + if (actual) die("%d: expected null, got \"%s\"", __LINE__, actual); int main() @@ -92,6 +99,7 @@ int main() struct root_opts opts = { .device = "/dev/hda1", .fstype = "xiafs", + .altdevice = NULL, .mount_opts = NULL }; char *buf; @@ -103,14 +111,22 @@ int main() expect_equal(opts.fstype, "ubifs"); expect_equal(opts.mount_opts, "subvol=1"); - // finds altroot= options - buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 root=/dev/ubi0_4 rootfstype=ubifs rootflags=subvol=1 fw_devlink=off mtdparts=phram0:18M(rootfs) phram.phram=phram0,0x40400000,18874368,65536 root=/dev/mtdblock0 altroot=/dev/mtdblock6 foo"); + // finds rootalt= options + buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 root=/dev/ubi0_4 rootfstype=ubifs rootflags=subvol=1 fw_devlink=off mtdparts=phram0:18M(rootfs) phram.phram=phram0,0x40400000,18874368,65536 root=/dev/mtdblock0 rootalt=/dev/mtdblock6 foo"); memset(&opts, '\0', sizeof opts); parseopts(buf, &opts); expect_equal(opts.device, "/dev/mtdblock0"); expect_equal(opts.altdevice, "/dev/mtdblock6"); expect_equal(opts.fstype, "ubifs"); expect_equal(opts.mount_opts, "subvol=1"); + // Ensure that `altdevice` is NULL. + buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 fw_devlink=off rootfstype=ubifs mtdparts=phram0:19791872(rootfs) phram.phram=phram0,33554432,19791872,65536 rootfstype=jffs2 root=/dev/mtdblock0"); + memset(&opts, '\0', sizeof opts); parseopts(buf, &opts); + expect_equal(opts.device, "/dev/mtdblock0"); + expect_null(opts.altdevice); + expect_equal(opts.fstype, "jffs2"); + expect_null(opts.mount_opts); + // in case of duplicates, chooses the latter // also: works if the option is end of string buf = strdup("liminix console=ttyS0,115200 panic=10 oops=panic init=/bin/init loglevel=8 root=/dev/ubi0_4 rootfstype=ubifs fw_devlink=off mtdparts=phram0:18M(rootfs) phram.phram=phram0,0x40400000,18874368,65536 root=/dev/mtdblock0"); @@ -146,12 +162,12 @@ int main() if(opts.altdevice) die("expected null altdevice, got \"%s\"", opts.altdevice); // provides empty strings for empty options - buf = strdup("liminix rootfstype= fw_devlink=off root= altroot= /dev/hda1"); + buf = strdup("liminix rootfstype= fw_devlink=off root= rootalt= /dev/hda1"); memset(&opts, '\0', sizeof opts); parseopts(buf, &opts); if(strlen(opts.fstype)) die("expected empty rootfstype, got \"%s\"", opts.fstype); if(strlen(opts.device)) die("expected empty root, got \"%s\"", opts.device); - if(strlen(opts.altdevice)) die("expected empty altroot, got \"%s\"", opts.altdevice); + if(strlen(opts.altdevice)) die("expected empty rootalt, got \"%s\"", opts.altdevice); expect_equal("01", pr_u32(1)); expect_equal("ab", pr_u32(0xab)); -- 2.47.0 From 3d528a71e97e8b6d238e1a3317de82ccc82e811f Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 26 Aug 2024 21:40:09 +0200 Subject: [PATCH 09/57] feat(hostapd): make the package configurable to enable RADIUS The default hostapd disable too many things, we need a bit more for RADIUS. Signed-off-by: Raito Bezarius --- modules/hostapd/default.nix | 4 ++++ modules/hostapd/service.nix | 5 ++--- overlay.nix | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/modules/hostapd/default.nix b/modules/hostapd/default.nix index 2bcf4d6..4338e74 100644 --- a/modules/hostapd/default.nix +++ b/modules/hostapd/default.nix @@ -26,6 +26,10 @@ in { interface = mkOption { type = liminix.lib.types.service; }; + package = mkOption { + type = types.package; + default = pkgs.hostapd; + }; params = mkOption { type = types.attrs; }; diff --git a/modules/hostapd/service.nix b/modules/hostapd/service.nix index 2bd5453..68e9754 100644 --- a/modules/hostapd/service.nix +++ b/modules/hostapd/service.nix @@ -1,10 +1,9 @@ { liminix -, hostapd , writeText , lib }: -{ interface, params} : +{ package, interface, params } : let inherit (liminix.services) longrun; inherit (lib) concatStringsSep mapAttrsToList; @@ -35,5 +34,5 @@ let in longrun { inherit name; dependencies = [ interface ]; - run = "${hostapd}/bin/hostapd -i $(output ${interface} ifname) -P /run/${name}.pid -S ${conf}"; + run = "${package}/bin/hostapd -i $(output ${interface} ifname) -P /run/${name}.pid -S ${conf}"; } diff --git a/overlay.nix b/overlay.nix index 09acaad..374c79a 100644 --- a/overlay.nix +++ b/overlay.nix @@ -143,6 +143,42 @@ extraPkgs // { }); in h.override { openssl = null; sqlite = null; }; + hostapd-radius = + let + config = [ + "CONFIG_DRIVER_NL80211=y" + "CONFIG_DRIVER_WIRED=y" + "CONFIG_EAP=y" + "CONFIG_EAP_PEAP=y" + "CONFIG_RADIUS_SERVER=y" + "CONFIG_FULL_DYNAMIC_VLAN=y" + "CONFIG_IAPP=y" + "CONFIG_IEEE80211AC=y" + "CONFIG_IEEE80211AX=y" + "CONFIG_IEEE80211N=y" + "CONFIG_IEEE80211W=y" + "CONFIG_INTERNAL_LIBTOMMATH=y" + "CONFIG_INTERNAL_LIBTOMMATH_FAST=y" + "CONFIG_IPV6=y" + "CONFIG_LIBNL32=y" + "CONFIG_PKCS12=y" + "CONFIG_RSN_PREAUTH=y" + # Required to read the key material for RADIUS. + "CONFIG_TLS=openssl" + ]; + h = prev.hostapd.overrideAttrs(o: { + extraConfig = ""; + configurePhase = '' + cat > hostapd/defconfig < Date: Sat, 31 Aug 2024 20:28:23 +0200 Subject: [PATCH 10/57] feat(mtd-utils): save more space Signed-off-by: Raito Bezarius --- overlay.nix | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/overlay.nix b/overlay.nix index 374c79a..2b46089 100644 --- a/overlay.nix +++ b/overlay.nix @@ -204,6 +204,11 @@ extraPkgs // { patches = (if o ? patches then o.patches else []) ++ [ ./pkgs/mtdutils/0001-mkfs.jffs2-add-graft-option.patch ]; + + postInstall = '' + # Testing programs which we don't need. We save a lot of space! + rm -rf $out/libexec + ''; }); nftables = prev.nftables.overrideAttrs(o: { -- 2.47.0 From f34a63d1c8df2ec751760f0fd1636d74bc2f8757 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 31 Aug 2024 21:23:17 +0200 Subject: [PATCH 11/57] feat(pki): init TLS PKI module Signed-off-by: Raito Bezarius --- modules/pki/default.nix | 83 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 modules/pki/default.nix diff --git a/modules/pki/default.nix b/modules/pki/default.nix new file mode 100644 index 0000000..b328f4d --- /dev/null +++ b/modules/pki/default.nix @@ -0,0 +1,83 @@ +{ config, lib, pkgs, ... }: +# Inspired from nixpkgs/NixOS. + +with lib; + +let + inherit (pkgs.pseudofile) dir symlink; + cfg = config.security.pki; + + cacertPackage = pkgs.cacert.override { + blacklist = [ ]; + extraCertificateFiles = cfg.certificateFiles; + extraCertificateStrings = cfg.certificates; + }; + caBundleName = "ca-bundle.crt"; + caBundle = "${cacertPackage}/etc/ssl/certs/${caBundleName}"; + +in + +{ + + options = { + security.pki.installCACerts = mkEnableOption "installing CA certificates to the system" // { + default = false; + }; + + security.pki.certificateFiles = mkOption { + type = types.listOf types.path; + default = []; + example = literalExpression ''[ "''${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt" ]''; + description = '' + A list of files containing trusted root certificates in PEM + format. These are concatenated to form + {file}`/etc/ssl/certs/ca-certificates.crt`, which is + used by many programs that use OpenSSL, such as + {command}`curl` and {command}`git`. + ''; + }; + + security.pki.certificates = mkOption { + type = types.listOf types.str; + default = []; + example = literalExpression '' + [ ''' + NixOS.org + ========= + -----BEGIN CERTIFICATE----- + MIIGUDCCBTigAwIBAgIDD8KWMA0GCSqGSIb3DQEBBQUAMIGMMQswCQYDVQQGEwJJ + TDEWMBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMiU2VjdXJlIERpZ2l0 + ... + -----END CERTIFICATE----- + ''' + ] + ''; + description = '' + A list of trusted root certificates in PEM format. + ''; + }; + }; + + config = mkIf cfg.installCACerts { + # NixOS canonical location + Debian/Ubuntu/Arch/Gentoo compatibility. + filesystem = dir { + etc = dir { + ssl = dir { + certs = dir { + "ca-certificates.crt" = symlink caBundle; + "ca-bundle.crt" = symlink caBundle; + }; + }; + + # CentOS/Fedora compatibility. + pki = dir { + certs = dir { + "ca-bundle.crt" = symlink caBundle; + }; + }; + }; + }; + }; + +} + -- 2.47.0 From 664624a478ff0952f5b2f87769de43552d888896 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 31 Aug 2024 20:44:22 +0200 Subject: [PATCH 12/57] feat(jitterentropy): introduce a jitterentropy module Signed-off-by: Raito Bezarius --- modules/all-modules.nix | 1 + modules/jitter-rng/default.nix | 21 +++++++++++++++++++++ modules/jitter-rng/jitter-rng.nix | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 modules/jitter-rng/default.nix create mode 100644 modules/jitter-rng/jitter-rng.nix diff --git a/modules/all-modules.nix b/modules/all-modules.nix index abce5db..98352c1 100644 --- a/modules/all-modules.nix +++ b/modules/all-modules.nix @@ -8,6 +8,7 @@ ./bridge ./busybox.nix ./dhcp6c + ./jitter-rng ./dnsmasq ./firewall ./hardware.nix diff --git a/modules/jitter-rng/default.nix b/modules/jitter-rng/default.nix new file mode 100644 index 0000000..aa6f9e5 --- /dev/null +++ b/modules/jitter-rng/default.nix @@ -0,0 +1,21 @@ +## CPU Jitter RNG +## ============== +## +## CPU Jitter RNG is a random number generator # providing non-physical true +## random generation # that works equally for kernel and user-land. It relies +## on the availability of a high-resolution timer. +{ lib, pkgs, ... }: +let + inherit (lib) mkOption types; + inherit (pkgs) liminix; +in { + options.system.service.jitter-rng = mkOption { + type = liminix.lib.types.serviceDefn; + }; + + config = { + system.service.jitter-rng = pkgs.liminix.callService ./jitter-rng.nix { + }; + }; +} + diff --git a/modules/jitter-rng/jitter-rng.nix b/modules/jitter-rng/jitter-rng.nix new file mode 100644 index 0000000..1072be1 --- /dev/null +++ b/modules/jitter-rng/jitter-rng.nix @@ -0,0 +1,18 @@ +{ + liminix +, lib +, jitterentropy-rngd +}: +{ }: +let + inherit (liminix.services) longrun; + name = "jitterentropy-rngd"; +in +longrun { + # Does it need to be unique? + inherit name; + run = '' + mkdir -p /run/jitterentropy-rngd + ${jitterentropy-rngd}/bin/jitterentropy-rngd -v -p /run/jitterentropy-rngd/${name}.pid + ''; +} -- 2.47.0 From 36f3015acdf58d37033e7d78fa6029b5fd2f0df5 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sat, 15 Jun 2024 15:34:49 +0100 Subject: [PATCH 13/57] dhcpc handle case when env vars are missing the notify-script should continue and signal readiness even if one or more of the outputs it writes are mssing in the environment --- modules/network/dhcpc.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/network/dhcpc.nix b/modules/network/dhcpc.nix index ed665b3..cd652b2 100644 --- a/modules/network/dhcpc.nix +++ b/modules/network/dhcpc.nix @@ -17,7 +17,7 @@ let ip address replace $ip/$mask dev $interface (in_outputs ${name} for i in lease mask ip router siaddr dns serverid subnet opt53 interface ; do - printenv $i > $i + (printenv $i || true) > $i done) } case $action in @@ -40,7 +40,7 @@ let ''; in longrun { inherit name; - run = "/bin/udhcpc -f -i $(output ${interface} ifname) -x hostname:$(cat /proc/sys/kernel/hostname) -s ${script}"; + run = "exec /bin/udhcpc -f -i $(output ${interface} ifname) -x hostname:$(cat /proc/sys/kernel/hostname) -s ${script}"; notification-fd = 10; dependencies = [ interface ]; } -- 2.47.0 From 1d0fc24111cab325ea65e7de12b2e14ccb51e93c Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sun, 1 Sep 2024 01:32:45 +0200 Subject: [PATCH 14/57] fix(bridge/members): log attach/detach Signed-off-by: Raito Bezarius --- modules/bridge/members.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/bridge/members.nix b/modules/bridge/members.nix index a278730..114bc2e 100644 --- a/modules/bridge/members.nix +++ b/modules/bridge/members.nix @@ -21,9 +21,13 @@ let service = oneshot { name = "${primary.name}.member.${member.name}"; up = '' + echo "attaching $(output ${member} ifname) to $(output ${primary} ifname) bridge" ip link set dev $(output ${member} ifname) master $(output ${primary} ifname) ''; - down = "ip link set dev $(output ${member} ifname) nomaster"; + down = '' + echo "detaching $(output ${member} ifname) from any bridge" + ip link set dev $(output ${member} ifname) nomaster + ''; }; }; in bundle { -- 2.47.0 From ca9642a61e87a00923df6801b4c3f0e366a72140 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sun, 1 Sep 2024 02:05:33 +0200 Subject: [PATCH 15/57] fix(ifwait): return :present if newlink is up/yes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Urgh… Signed-off-by: Raito Bezarius --- pkgs/ifwait/ifwait.fnl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/ifwait/ifwait.fnl b/pkgs/ifwait/ifwait.fnl index cc5e76c..52122bd 100644 --- a/pkgs/ifwait/ifwait.fnl +++ b/pkgs/ifwait/ifwait.fnl @@ -23,7 +23,7 @@ {:present true :up true :running true} {:event "newlink" :name params.link :up :yes} - {:present :true :up true} + {:present true :up true} {:event "newlink" :name params.link} {:present true } -- 2.47.0 From f0b4e826cb32f5a66b67bc002b2753ffa8c285dc Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sun, 1 Sep 2024 02:13:36 +0200 Subject: [PATCH 16/57] fix(ifwait): match over strings and not symbols Are they the same in Fennel? Signed-off-by: Raito Bezarius --- pkgs/ifwait/ifwait.fnl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/ifwait/ifwait.fnl b/pkgs/ifwait/ifwait.fnl index 52122bd..f835ebb 100644 --- a/pkgs/ifwait/ifwait.fnl +++ b/pkgs/ifwait/ifwait.fnl @@ -19,10 +19,10 @@ (match v ;; - up: Reflects the administrative state of the interface (IFF_UP) ;; - running: Reflects the operational state (IFF_RUNNING). - {:event "newlink" :name params.link :up :yes :running :yes} + {:event "newlink" :name params.link :up "yes" :running "yes"} {:present true :up true :running true} - {:event "newlink" :name params.link :up :yes} + {:event "newlink" :name params.link :up "yes"} {:present true :up true} {:event "newlink" :name params.link} -- 2.47.0 From 21ff11503eb283f52bc5abc47fbb05d80bae0d01 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sun, 1 Sep 2024 14:56:08 +0200 Subject: [PATCH 17/57] fix(bridge): pick up MAC from another interface This avoids the OPERSTATE unknown when the bridge is brought up but the members are not ready yet. This will make OPERSTATE to down, enabling us to wait until we have brought up completely all the members. Signed-off-by: Raito Bezarius --- modules/bridge/default.nix | 6 ++++++ modules/bridge/primary.nix | 11 ++++++++--- pkgs/liminix-tools/networking/default.nix | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/bridge/default.nix b/modules/bridge/default.nix index 32df4a5..fd779f8 100644 --- a/modules/bridge/default.nix +++ b/modules/bridge/default.nix @@ -28,6 +28,12 @@ in type = types.str; description = "bridge interface name to create"; }; + + macAddressFromInterface = mkOption { + type = types.nullOr liminix.lib.types.service; + default = null; + description = "reuse mac address from an existing interface service"; + }; }; members = config.system.callService ./members.nix { primary = mkOption { diff --git a/modules/bridge/primary.nix b/modules/bridge/primary.nix index c25e5fe..aa7822c 100644 --- a/modules/bridge/primary.nix +++ b/modules/bridge/primary.nix @@ -3,15 +3,20 @@ , ifwait , lib }: -{ ifname } : +{ ifname, macAddressFromInterface ? null } : let inherit (liminix.services) bundle oneshot; - inherit (lib) mkOption types; + inherit (lib) mkOption types optional; in oneshot rec { name = "${ifname}.link"; up = '' - ip link add name ${ifname} type bridge + ${if macAddressFromInterface == null then + "ip link add name ${ifname} type bridge" + else + "ip link add name ${ifname} address $(output ${macAddressFromInterface} ether) type bridge"} ${liminix.networking.ifup name ifname} ''; down = "ip link set down dev ${ifname}"; + + dependencies = optional (macAddressFromInterface != null) macAddressFromInterface; } diff --git a/pkgs/liminix-tools/networking/default.nix b/pkgs/liminix-tools/networking/default.nix index aed7c4e..b51b367 100644 --- a/pkgs/liminix-tools/networking/default.nix +++ b/pkgs/liminix-tools/networking/default.nix @@ -9,6 +9,7 @@ ip link set up dev ${ifname} (in_outputs ${name} echo ${ifname} > ifname + cat /sys/class/net/${ifname}/address > ether ) ''; } -- 2.47.0 From 1bd9af1e9d38d8b241f1d838e4af98e1b13b958b Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sun, 1 Sep 2024 15:42:26 +0200 Subject: [PATCH 18/57] fix(bridge): reorder initialization for bridge dependents Consider the scenario where you run DHCPv4 on the primary bridge interface. You have no real interface to "wait upon", so it's OK. Nonetheless, anything depending on successful completion of DHCPv4, e.g. adding a default route, will block `s6-rc -v2 up change default`. The way new interfaces are attached to the bridge is via `s6-rc -b -u change $attach-oneshot-service`, this introduce in turn a deadlock. At some point, DHCPv4 will timeout, unblocking the deadlock and attaching the members to the primary bridge interface, making it ready to send L2 broadcast packets for DHCP, unblocking DHCP in turn again. This is not satisfying because we really want to have a no-hiccups bring-up. To fix this, we proceed to multiple changes: - we remove `svc.ifwait.build` out of band `s6-rc -b -u $oneshot-attach` call, which is, by design, wrong here. - users can now depend on the members service to know when a bridge is fully operational (we could make it more granular and let them depend on the LAN member joining rather than WLAN, etc.) - users can also depend on the primary service being brought up rather than just being present, this is useful if you need to bring it up when it has AT LEAST one member to get link local address or MAC addresses (fixing DHCPv6 bring up as well because `ff02::1` is used there). One thing is not addressed yet, if you are running a WLAN service using RADIUS attached to the bridge, at bring up time, it will try to reach out the external RADIUS server and *fail*. To solve this, granular dependency on the DHCPv4 once LAN is joined. Then the hostapd can wait on defaultroute4 completion so that connectivity is available to reach RADIUS server. It can join the bridge later on without any hiccup as well. This is left as a TODO as hostapd can survive RADIUS authentication failure and retry later. Signed-off-by: Raito Bezarius --- modules/bridge/default.nix | 14 ++++++++++++++ modules/bridge/members.nix | 33 +++++++++++++-------------------- modules/bridge/primary.nix | 8 ++++++-- modules/bridge/ready.nix | 18 ++++++++++++++++++ 4 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 modules/bridge/ready.nix diff --git a/modules/bridge/default.nix b/modules/bridge/default.nix index fd779f8..4c2911a 100644 --- a/modules/bridge/default.nix +++ b/modules/bridge/default.nix @@ -20,6 +20,7 @@ in system.service.bridge = { primary = mkOption { type = liminix.lib.types.serviceDefn; }; members = mkOption { type = liminix.lib.types.serviceDefn; }; + ready = mkOption { type = liminix.lib.types.serviceDefn; }; }; }; config.system.service.bridge = { @@ -46,6 +47,19 @@ in description = "interfaces to add to the bridge"; }; }; + + # TODO: generalize it outside + ready = config.system.callService ./ready.nix { + primary = mkOption { + type = liminix.lib.types.service; + description = "primary bridge interface"; + }; + + members = mkOption { + type = liminix.lib.types.service; + description = "members service"; + }; + }; }; config.kernel.config = { BRIDGE = "y"; diff --git a/modules/bridge/members.nix b/modules/bridge/members.nix index 114bc2e..2171eda 100644 --- a/modules/bridge/members.nix +++ b/modules/bridge/members.nix @@ -10,26 +10,19 @@ let inherit (liminix.networking) interface; inherit (liminix.services) bundle oneshot; inherit (lib) mkOption types; - addif = member : - # how do we get sight of services from here? maybe we need to - # implement ifwait as a regualr derivation instead of a - # servicedefinition - svc.ifwait.build { - state = "running"; - interface = member; - dependencies = [ primary member ]; - service = oneshot { - name = "${primary.name}.member.${member.name}"; - up = '' - echo "attaching $(output ${member} ifname) to $(output ${primary} ifname) bridge" - ip link set dev $(output ${member} ifname) master $(output ${primary} ifname) - ''; - down = '' - echo "detaching $(output ${member} ifname) from any bridge" - ip link set dev $(output ${member} ifname) nomaster - ''; - }; - }; + addif = member : oneshot { + name = "${primary.name}.member.${member.name}"; + up = '' + echo "attaching $(output ${member} ifname) to $(output ${primary} ifname) bridge" + ip link set dev $(output ${member} ifname) master $(output ${primary} ifname) + ''; + down = '' + echo "detaching $(output ${member} ifname) from any bridge" + ip link set dev $(output ${member} ifname) nomaster + ''; + + dependencies = [ primary member ]; + }; in bundle { name = "${primary.name}.members"; contents = map addif members; diff --git a/modules/bridge/primary.nix b/modules/bridge/primary.nix index aa7822c..f5e1219 100644 --- a/modules/bridge/primary.nix +++ b/modules/bridge/primary.nix @@ -14,9 +14,13 @@ in oneshot rec { "ip link add name ${ifname} type bridge" else "ip link add name ${ifname} address $(output ${macAddressFromInterface} ether) type bridge"} - ${liminix.networking.ifup name ifname} + + (in_outputs ${name} + echo ${ifname} > ifname + cat /sys/class/net/${ifname}/address > ether + ) ''; - down = "ip link set down dev ${ifname}"; + down = "ip link delete ${ifname}"; dependencies = optional (macAddressFromInterface != null) macAddressFromInterface; } diff --git a/modules/bridge/ready.nix b/modules/bridge/ready.nix new file mode 100644 index 0000000..f8c40b3 --- /dev/null +++ b/modules/bridge/ready.nix @@ -0,0 +1,18 @@ +{ + liminix +, ifwait +, lib +}: +{ primary, members } : +let + inherit (liminix.services) oneshot; +in oneshot { + name = "${primary.name}.oper"; + up = '' + ip link set up dev $(output ${primary} ifname) + ${ifwait}/bin/ifwait -v $(output ${primary} ifname) running + ''; + down = "ip link set down dev $(output ${primary} ifname)"; + + dependencies = [ members ]; +} -- 2.47.0 From eb083bee203f584542ecf79ce229e21b25cebb1b Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Wed, 4 Sep 2024 15:53:19 +0200 Subject: [PATCH 19/57] libubox: init at unstable-04-09-2024 Signed-off-by: Raito Bezarius --- pkgs/default.nix | 1 + pkgs/libubox/default.nix | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 pkgs/libubox/default.nix diff --git a/pkgs/default.nix b/pkgs/default.nix index 7bb2470..3c372c7 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -110,6 +110,7 @@ in { swconfig = callPackage ./swconfig {}; systemconfig = callPackage ./systemconfig {}; tufted = callPackage ./tufted {}; + libubox = callPackage ./libubox {}; uevent-watch = callPackage ./uevent-watch {}; usb-modeswitch = callPackage ./usb-modeswitch {}; writeAshScript = callPackage ./write-ash-script {}; diff --git a/pkgs/libubox/default.nix b/pkgs/libubox/default.nix new file mode 100644 index 0000000..6c68e3e --- /dev/null +++ b/pkgs/libubox/default.nix @@ -0,0 +1,46 @@ +{ + lib, + stdenv, + fetchgit, + cmake, + lua5_1, + json_c +}: + +stdenv.mkDerivation rec { + pname = "libubox"; + version = "unstable-2024-04-09"; + + src = fetchgit { + url = "https://git.openwrt.org/project/libubox.git"; + rev = "eb9bcb64185ac155c02cc1a604692c4b00368324"; + hash = "sha256-5KO2E+4pcDp/pe2+vjoQDmyMwCc0yKm847U4J6HjxyA="; + }; + + nativeBuildInputs = [ + cmake + lua5_1 + ]; + + buildInputs = [ + lua5_1 + json_c + ]; + + # Otherwise, CMake cannot find jsoncpp? + env.NIX_CFLAGS_COMPILE = toString [ "-I${json_c.dev}/include/json-c" "-D JSONC" ]; + + cmakeFlags = [ + "-DBUILD_EXAMPLES=off" + # TODO: it explode at install phase. + "-DBUILD_LUA=off" + ]; + + meta = { + description = ""; + homepage = "https://git.openwrt.org/project/libubox.git"; + maintainers = with lib.maintainers; [ raitobezarius ]; + mainProgram = "libubox"; + platforms = lib.platforms.all; + }; +} -- 2.47.0 From 4287a051823e7444fcf997e2a685a9df5e968b28 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Wed, 4 Sep 2024 15:53:26 +0200 Subject: [PATCH 20/57] ubus: init at unstable-04-09-2024 Signed-off-by: Raito Bezarius --- pkgs/default.nix | 1 + pkgs/ubus/default.nix | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 pkgs/ubus/default.nix diff --git a/pkgs/default.nix b/pkgs/default.nix index 3c372c7..e719d4e 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -111,6 +111,7 @@ in { systemconfig = callPackage ./systemconfig {}; tufted = callPackage ./tufted {}; libubox = callPackage ./libubox {}; + ubus = callPackage ./ubus {}; uevent-watch = callPackage ./uevent-watch {}; usb-modeswitch = callPackage ./usb-modeswitch {}; writeAshScript = callPackage ./write-ash-script {}; diff --git a/pkgs/ubus/default.nix b/pkgs/ubus/default.nix new file mode 100644 index 0000000..37bd457 --- /dev/null +++ b/pkgs/ubus/default.nix @@ -0,0 +1,26 @@ +{ stdenv, lib, fetchFromGitHub, cmake, libubox, json_c }: +stdenv.mkDerivation { + pname = "ubus"; + version = "unstable-04-09-2024"; + + src = fetchFromGitHub { + owner = "openwrt"; + repo = "ubus"; + rev = "65bb027054def3b94a977229fd6ad62ddd32345b"; + hash = "sha256-n82Ub0IiuvWbnlDCoN+0hjo/1PbplEbc56kuOYMrHxQ="; + }; + + nativeBuildInputs = [ + cmake + ]; + + buildInputs = [ + libubox + json_c + ]; + + cmakeFlags = [ + "-DBUILD_LUA=off" + "-DBUILD_EXAMPLES=off" + ]; +} -- 2.47.0 From c24c659ee18c4652ae17f25cb1657323f4cda14c Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Wed, 4 Sep 2024 16:07:49 +0200 Subject: [PATCH 21/57] modules/ubus: init Signed-off-by: Raito Bezarius --- modules/ubus/default.nix | 24 ++++++++++++++++++++++++ modules/ubus/service.nix | 13 +++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 modules/ubus/default.nix create mode 100644 modules/ubus/service.nix diff --git a/modules/ubus/default.nix b/modules/ubus/default.nix new file mode 100644 index 0000000..827f537 --- /dev/null +++ b/modules/ubus/default.nix @@ -0,0 +1,24 @@ +## ubus +## ==== +## +## ubus is a micro-bus à la D-Bus for all your needs. + +{ lib, pkgs, config, ...}: +let + inherit (lib) mkOption types; + inherit (pkgs) liminix; +in { + options = { + system.service.ubus = mkOption { + type = liminix.lib.types.serviceDefn; + }; + }; + config = { + system.service.ubus = liminix.callService ./service.nix { + package = mkOption { + type = types.package; + default = pkgs.ubus; + }; + }; + }; +} diff --git a/modules/ubus/service.nix b/modules/ubus/service.nix new file mode 100644 index 0000000..e1e297e --- /dev/null +++ b/modules/ubus/service.nix @@ -0,0 +1,13 @@ +{ + liminix +, writeText +, lib +}: +{ package } : +let + inherit (liminix.services) longrun; +in longrun { + # Long term: make it unique so that user can spawn multiple buses if they want. + name = "ubus"; + run = "${package}/bin/ubusd"; +} -- 2.47.0 From 6d4237ff87967209e6b53be8c57a622956fc9fee Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Wed, 4 Sep 2024 16:29:59 +0200 Subject: [PATCH 22/57] libubox: build with Lua Signed-off-by: Raito Bezarius --- pkgs/libubox/default.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/libubox/default.nix b/pkgs/libubox/default.nix index 6c68e3e..43b0377 100644 --- a/pkgs/libubox/default.nix +++ b/pkgs/libubox/default.nix @@ -33,7 +33,8 @@ stdenv.mkDerivation rec { cmakeFlags = [ "-DBUILD_EXAMPLES=off" # TODO: it explode at install phase. - "-DBUILD_LUA=off" + "-DBUILD_LUA=on" + "-DLUAPATH=${placeholder "out"}/lib/lua" ]; meta = { -- 2.47.0 From 78d0088b650926f70429d609095697f6a868d54c Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Wed, 4 Sep 2024 16:30:44 +0200 Subject: [PATCH 23/57] ubus: build with Lua Signed-off-by: Raito Bezarius --- pkgs/ubus/default.nix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkgs/ubus/default.nix b/pkgs/ubus/default.nix index 37bd457..c5c78a1 100644 --- a/pkgs/ubus/default.nix +++ b/pkgs/ubus/default.nix @@ -1,4 +1,4 @@ -{ stdenv, lib, fetchFromGitHub, cmake, libubox, json_c }: +{ stdenv, fetchFromGitHub, cmake, libubox, json_c, lua5_1 }: stdenv.mkDerivation { pname = "ubus"; version = "unstable-04-09-2024"; @@ -15,12 +15,14 @@ stdenv.mkDerivation { ]; buildInputs = [ + lua5_1 libubox json_c ]; cmakeFlags = [ - "-DBUILD_LUA=off" + "-DBUILD_LUA=on" + "-DLUAPATH=${placeholder "out"}/lib/lua" "-DBUILD_EXAMPLES=off" ]; } -- 2.47.0 From ebcdbf76bcb41b3fd8ba5198ca9b61e33c5e9feb Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Thu, 5 Sep 2024 14:27:47 +0200 Subject: [PATCH 24/57] fix(bridge): members are now granular services They are still part of the bundle, but we can wait on each of them independently now. Signed-off-by: Raito Bezarius --- modules/bridge/default.nix | 16 ++++++++++++++-- modules/bridge/members.nix | 11 +++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/modules/bridge/default.nix b/modules/bridge/default.nix index 4c2911a..3a24d7c 100644 --- a/modules/bridge/default.nix +++ b/modules/bridge/default.nix @@ -43,8 +43,20 @@ in }; members = mkOption { - type = types.listOf liminix.lib.types.interface; - description = "interfaces to add to the bridge"; + type = types.attrsOf (types.submodule ({ ... }: { options = { + member = mkOption { + type = liminix.lib.types.interface; + description = "interface to add"; + }; + + dependencies = mkOption { + type = types.listOf liminix.lib.types.service; + default = []; + description = "extra dependencies before attaching this interface to the bridge"; + }; + }; })); + + description = "set of bridge members"; }; }; diff --git a/modules/bridge/members.nix b/modules/bridge/members.nix index 2171eda..78c64e2 100644 --- a/modules/bridge/members.nix +++ b/modules/bridge/members.nix @@ -7,11 +7,10 @@ { members, primary } : let - inherit (liminix.networking) interface; inherit (liminix.services) bundle oneshot; - inherit (lib) mkOption types; - addif = member : oneshot { - name = "${primary.name}.member.${member.name}"; + inherit (lib) mapAttrs; + addif = name: { dependencies ? [ ], member }: oneshot { + name = "${primary.name}.member.${name}"; up = '' echo "attaching $(output ${member} ifname) to $(output ${primary} ifname) bridge" ip link set dev $(output ${member} ifname) master $(output ${primary} ifname) @@ -21,9 +20,9 @@ let ip link set dev $(output ${member} ifname) nomaster ''; - dependencies = [ primary member ]; + dependencies = [ primary member ] ++ dependencies; }; in bundle { name = "${primary.name}.members"; - contents = map addif members; + contents = builtins.attrValues (mapAttrs addif members); } -- 2.47.0 From 0fb671023c0571d936a568721c67819a23cf63c0 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Thu, 5 Sep 2024 14:50:51 +0200 Subject: [PATCH 25/57] feat(services): introduce structured bundles Structured bundles keep their original contents as a `passthru` field named `components`. This enable users to depend on a specific piece of the bundle instead of the whole bundle. Signed-off-by: Raito Bezarius --- modules/bridge/members.nix | 6 +++--- pkgs/liminix-tools/services/default.nix | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/modules/bridge/members.nix b/modules/bridge/members.nix index 78c64e2..560ba14 100644 --- a/modules/bridge/members.nix +++ b/modules/bridge/members.nix @@ -7,7 +7,7 @@ { members, primary } : let - inherit (liminix.services) bundle oneshot; + inherit (liminix.services) structuredBundle oneshot; inherit (lib) mapAttrs; addif = name: { dependencies ? [ ], member }: oneshot { name = "${primary.name}.member.${name}"; @@ -22,7 +22,7 @@ let dependencies = [ primary member ] ++ dependencies; }; -in bundle { +in structuredBundle { name = "${primary.name}.members"; - contents = builtins.attrValues (mapAttrs addif members); + contents = mapAttrs addif members; } diff --git a/pkgs/liminix-tools/services/default.nix b/pkgs/liminix-tools/services/default.nix index c0dbaf0..466a978 100644 --- a/pkgs/liminix-tools/services/default.nix +++ b/pkgs/liminix-tools/services/default.nix @@ -39,6 +39,7 @@ let , contents ? [] , buildInputs ? [] , isTrigger ? false + , passthru ? {} } @ args: stdenvNoCC.mkDerivation { # we use stdenvNoCC to avoid generating derivations with names @@ -50,6 +51,8 @@ let dependencies = builtins.map (d: d.name) dependencies; contents = builtins.map (d: d.name) contents; builder = ./builder.sh; + + inherit passthru; }; longrun = { @@ -100,7 +103,18 @@ let serviceType = "bundle"; inherit contents dependencies; }); + structuredBundle = { + name + , contents ? {} + , dependencies ? [] + , ... + } @ args: service (args // { + serviceType = "bundle"; + contents = builtins.attrValues contents; + inherit dependencies; + passthru.components = contents; + }); target = bundle; in { - inherit target bundle oneshot longrun output; + inherit target bundle oneshot longrun output structuredBundle; } -- 2.47.0 From 032e57b34ced7e7ed5f65be60d1d1c6a47dd6aa6 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Thu, 5 Sep 2024 18:32:11 +0200 Subject: [PATCH 26/57] feat(hostapd): ubus support Signed-off-by: Raito Bezarius --- overlay.nix | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/overlay.nix b/overlay.nix index 2b46089..4fd607c 100644 --- a/overlay.nix +++ b/overlay.nix @@ -130,9 +130,18 @@ extraPkgs // { "CONFIG_LIBNL32=y" "CONFIG_PKCS12=y" "CONFIG_RSN_PREAUTH=y" + "CONFIG_UBUS=y" "CONFIG_TLS=internal" ]; h = prev.hostapd.overrideAttrs(o: { + buildInputs = o.buildInputs ++ [ final.libubox final.ubus ]; + src = final.fetchFromGitea { + domain = "git.dgnum.eu"; + owner = "DGNum"; + repo = "hostapd"; + rev = "hostap-liminix-integration"; + hash = "sha256-qoCXx3raXCD51YX5izj30VG/HMgr6lv/288Yg9I4S7M="; + }; extraConfig = ""; configurePhase = '' cat > hostapd/defconfig < Date: Sat, 7 Sep 2024 17:03:50 +0200 Subject: [PATCH 27/57] feat(hostapd): disable openssl to save space Signed-off-by: Raito Bezarius --- overlay.nix | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/overlay.nix b/overlay.nix index 4fd607c..dc6006a 100644 --- a/overlay.nix +++ b/overlay.nix @@ -172,8 +172,7 @@ extraPkgs // { "CONFIG_LIBNL32=y" "CONFIG_PKCS12=y" "CONFIG_RSN_PREAUTH=y" - # Required to read the key material for RADIUS. - "CONFIG_TLS=openssl" + "CONFIG_TLS=internal" ]; h = prev.hostapd.overrideAttrs(o: { extraConfig = ""; @@ -184,7 +183,7 @@ extraPkgs // { ${o.configurePhase} ''; }); - in h.override { sqlite = null; }; + in h.override { openssl = null; sqlite = null; }; -- 2.47.0 From ffc6492365951ae924e8a1baea344cc7f89f9b20 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 17:13:29 +0200 Subject: [PATCH 28/57] fix(ubus): set the socket path properly Signed-off-by: Raito Bezarius --- modules/ubus/service.nix | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/ubus/service.nix b/modules/ubus/service.nix index e1e297e..80548c6 100644 --- a/modules/ubus/service.nix +++ b/modules/ubus/service.nix @@ -9,5 +9,8 @@ let in longrun { # Long term: make it unique so that user can spawn multiple buses if they want. name = "ubus"; - run = "${package}/bin/ubusd"; + run = '' + mkdir -p /run/ubus + ${package}/bin/ubusd -s /run/ubus/ubus.sock + ''; } -- 2.47.0 From 5444059b6324ba9ccea5a37926df95fa3d7fa055 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 17:18:59 +0200 Subject: [PATCH 29/57] feat(hostapd): enable ubus on RADIUS variant Signed-off-by: Raito Bezarius --- overlay.nix | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/overlay.nix b/overlay.nix index dc6006a..6032257 100644 --- a/overlay.nix +++ b/overlay.nix @@ -172,9 +172,18 @@ extraPkgs // { "CONFIG_LIBNL32=y" "CONFIG_PKCS12=y" "CONFIG_RSN_PREAUTH=y" + "CONFIG_UBUS=y" "CONFIG_TLS=internal" ]; h = prev.hostapd.overrideAttrs(o: { + buildInputs = o.buildInputs ++ [ final.libubox final.ubus ]; + src = final.fetchFromGitea { + domain = "git.dgnum.eu"; + owner = "DGNum"; + repo = "hostapd"; + rev = "hostap-liminix-integration"; + hash = "sha256-qoCXx3raXCD51YX5izj30VG/HMgr6lv/288Yg9I4S7M="; + }; extraConfig = ""; configurePhase = '' cat > hostapd/defconfig < Date: Sat, 7 Sep 2024 17:35:09 +0200 Subject: [PATCH 30/57] fix(ubus): rendez vous URL for the unix socket Signed-off-by: Raito Bezarius --- pkgs/ubus/default.nix | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkgs/ubus/default.nix b/pkgs/ubus/default.nix index c5c78a1..88ea647 100644 --- a/pkgs/ubus/default.nix +++ b/pkgs/ubus/default.nix @@ -1,4 +1,4 @@ -{ stdenv, fetchFromGitHub, cmake, libubox, json_c, lua5_1 }: +{ stdenv, fetchFromGitHub, cmake, libubox, json_c, lua5_1, defaultSocketLocation ? "/run/ubus/ubus.sock" }: stdenv.mkDerivation { pname = "ubus"; version = "unstable-04-09-2024"; @@ -10,6 +10,12 @@ stdenv.mkDerivation { hash = "sha256-n82Ub0IiuvWbnlDCoN+0hjo/1PbplEbc56kuOYMrHxQ="; }; + # We don't use /var/run/ in Liminix by default. + postPatch = '' + substituteInPlace CMakeLists.txt \ + --replace-fail "/var/run/ubus/ubus.sock" "${defaultSocketLocation}" + ''; + nativeBuildInputs = [ cmake ]; -- 2.47.0 From 0d36000d9ff34a85a613e510e2d7ab1faaa31fd4 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 18:09:34 +0200 Subject: [PATCH 31/57] overlay: update hostapd with readiness support Signed-off-by: Raito Bezarius --- overlay.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/overlay.nix b/overlay.nix index 6032257..8ecfa5e 100644 --- a/overlay.nix +++ b/overlay.nix @@ -140,7 +140,7 @@ extraPkgs // { owner = "DGNum"; repo = "hostapd"; rev = "hostap-liminix-integration"; - hash = "sha256-qoCXx3raXCD51YX5izj30VG/HMgr6lv/288Yg9I4S7M="; + hash = "sha256-5Xi90keCHxvuKR5Q7STuZDzuM9h9ac6aWoXVQYvqkQI="; }; extraConfig = ""; configurePhase = '' @@ -182,7 +182,7 @@ extraPkgs // { owner = "DGNum"; repo = "hostapd"; rev = "hostap-liminix-integration"; - hash = "sha256-qoCXx3raXCD51YX5izj30VG/HMgr6lv/288Yg9I4S7M="; + hash = "sha256-5Xi90keCHxvuKR5Q7STuZDzuM9h9ac6aWoXVQYvqkQI="; }; extraConfig = ""; configurePhase = '' -- 2.47.0 From 4cf10c2e756dd293fbbd385c42bb8f658ee9286e Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 18:51:11 +0200 Subject: [PATCH 32/57] iwinfo: init at unstable-07-09-2024 Signed-off-by: Raito Bezarius --- pkgs/default.nix | 1 + pkgs/iwinfo/default.nix | 59 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 pkgs/iwinfo/default.nix diff --git a/pkgs/default.nix b/pkgs/default.nix index e719d4e..75ecb28 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -112,6 +112,7 @@ in { tufted = callPackage ./tufted {}; libubox = callPackage ./libubox {}; ubus = callPackage ./ubus {}; + iwinfo = callPackage ./iwinfo {}; uevent-watch = callPackage ./uevent-watch {}; usb-modeswitch = callPackage ./usb-modeswitch {}; writeAshScript = callPackage ./write-ash-script {}; diff --git a/pkgs/iwinfo/default.nix b/pkgs/iwinfo/default.nix new file mode 100644 index 0000000..cb5f964 --- /dev/null +++ b/pkgs/iwinfo/default.nix @@ -0,0 +1,59 @@ +{ + lib, + stdenv, + fetchFromGitea, + ubus, + libubox, + lua5_1, + libnl-tiny, + backend ? "nl80211" +}: +let + lua = lua5_1; +in +stdenv.mkDerivation rec { + pname = "iwinfo"; + version = "unstable-07-09-2024"; + + src = fetchFromGitea { + domain = "git.dgnum.eu"; + owner = "DGNum"; + repo = "iwinfo"; + rev = "aaa40cc77a42683df3c32d1f693e9ab95f910f2a"; + hash = "sha256-ojBj0dbDgtnloe1MILIjYVPX3PVZeUbYnEdqeg6/4OM="; + }; + + BACKENDS = backend; + + buildInputs = [ + ubus + libubox + lua + libnl-tiny + ]; + + CFLAGS = "-I${libnl-tiny}/include/libnl-tiny -D_GNU_SOURCE"; + + installPhase = '' + runHook preInstall + + install -Dm755 iwinfo $out/bin/iwinfo + install -Dm755 iwinfo.so $out/lib/lua/${lua.luaversion}/iwinfo.so + install -Dm755 libiwinfo.so $out/lib/libiwinfo.so + install -Dm755 libiwinfo.so.0 $out/lib/libiwinfo.so.0 + + mkdir -p $out/include + cp -r include/* $out/include + + runHook postInstall + ''; + + meta = { + description = "Library to access wireless devices"; + homepage = "https://github.com/openwrt/iwinfo"; + license = lib.licenses.gpl2Only; + maintainers = with lib.maintainers; [ raitobezarius ]; + mainProgram = "iwinfo"; + platforms = lib.platforms.all; + }; +} -- 2.47.0 From b57df3f288069615691d6f1b9755637895dd5bd8 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 19:41:13 +0200 Subject: [PATCH 33/57] iwinfo: use Lua 5.3 port Signed-off-by: Raito Bezarius --- pkgs/iwinfo/default.nix | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkgs/iwinfo/default.nix b/pkgs/iwinfo/default.nix index cb5f964..91c8437 100644 --- a/pkgs/iwinfo/default.nix +++ b/pkgs/iwinfo/default.nix @@ -4,12 +4,12 @@ fetchFromGitea, ubus, libubox, - lua5_1, + lua5_3, libnl-tiny, backend ? "nl80211" }: let - lua = lua5_1; + lua = lua5_3; in stdenv.mkDerivation rec { pname = "iwinfo"; @@ -19,8 +19,8 @@ stdenv.mkDerivation rec { domain = "git.dgnum.eu"; owner = "DGNum"; repo = "iwinfo"; - rev = "aaa40cc77a42683df3c32d1f693e9ab95f910f2a"; - hash = "sha256-ojBj0dbDgtnloe1MILIjYVPX3PVZeUbYnEdqeg6/4OM="; + rev = "14685a26805155aa5c137993b9a4861a0bc585d5"; + hash = "sha256-lg4sBoYcFFLhcUv+wKR6u1OCartjtnAoF9M5FdfO6JE="; }; BACKENDS = backend; -- 2.47.0 From 26d2812aa4985d9b3f181ae678a5b2cfbf57074f Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 19:44:31 +0200 Subject: [PATCH 34/57] fennerepl: remove readline Signed-off-by: Raito Bezarius --- pkgs/fennelrepl/default.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/pkgs/fennelrepl/default.nix b/pkgs/fennelrepl/default.nix index 152c605..f35abc9 100644 --- a/pkgs/fennelrepl/default.nix +++ b/pkgs/fennelrepl/default.nix @@ -20,7 +20,6 @@ let packages = [ lualinux netlink-lua iwinfo - lua.pkgs.readline ]; join = ps: builtins.concatStringsSep ";" ps; luapath = join (builtins.map (f: -- 2.47.0 From 0c363be423ad00af5dc1f9abe9779f75a59c3b04 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 19:41:06 +0200 Subject: [PATCH 35/57] fennelrepl: add iwinfo to it Signed-off-by: Raito Bezarius --- pkgs/fennelrepl/default.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/fennelrepl/default.nix b/pkgs/fennelrepl/default.nix index 28f47ca..152c605 100644 --- a/pkgs/fennelrepl/default.nix +++ b/pkgs/fennelrepl/default.nix @@ -10,6 +10,7 @@ , linotify , anoia , netlink-lua +, iwinfo , fennel }: let packages = [ @@ -18,6 +19,7 @@ let packages = [ fennel lualinux netlink-lua + iwinfo lua.pkgs.readline ]; join = ps: builtins.concatStringsSep ";" ps; -- 2.47.0 From c39bfc2bb56bbb7f4fb5b31d5e8fb9035b010709 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 20:59:51 +0200 Subject: [PATCH 36/57] fix(write-fennel): escape binary path for PATH otherwise, escapeShellArg might just do nothing! Signed-off-by: Raito Bezarius --- pkgs/write-fennel/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/write-fennel/default.nix b/pkgs/write-fennel/default.nix index 67d1908..e72762d 100644 --- a/pkgs/write-fennel/default.nix +++ b/pkgs/write-fennel/default.nix @@ -27,7 +27,7 @@ name : echo "#!${lua}/bin/lua ${luaFlags}" echo "package.path = ${lib.strings.escapeShellArg (builtins.concatStringsSep "" luapath)} .. package.path" echo "package.cpath = ${lib.strings.escapeShellArg (builtins.concatStringsSep "" luacpath)} .. package.cpath" - echo "local ok, stdlib = pcall(require,'posix.stdlib'); if ok then stdlib.setenv('PATH',${lib.escapeShellArg (lib.makeBinPath packages)} .. \":\" .. os.getenv('PATH')) end" + echo "local ok, stdlib = pcall(require,'posix.stdlib'); if ok then stdlib.setenv('PATH', \"${lib.makeBinPath packages}\" .. \":\" .. os.getenv('PATH')) end" fennel ${if correlate then "--correlate" else ""} --compile ${source} ) > ${name}.lua ''; -- 2.47.0 From 5590fea16e6e14de9084cdad7f0804d2bef74279 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 20:26:40 +0200 Subject: [PATCH 37/57] feat(ifwait): add ifbridgeable script This enable to wait for bridgeability of a WLAN interface via `iwinfo`. Signed-off-by: Raito Bezarius --- pkgs/anoia/Makefile | 2 +- pkgs/anoia/default.nix | 3 ++- pkgs/anoia/wlan.fnl | 8 ++++++++ pkgs/ifwait/default.nix | 5 ++++- pkgs/ifwait/ifbridgeable.fnl | 30 ++++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 pkgs/anoia/wlan.fnl create mode 100644 pkgs/ifwait/ifbridgeable.fnl diff --git a/pkgs/anoia/Makefile b/pkgs/anoia/Makefile index f68e596..1c1a406 100644 --- a/pkgs/anoia/Makefile +++ b/pkgs/anoia/Makefile @@ -1,5 +1,5 @@ -default: fs.lua init.lua nl.lua svc.lua net/constants.lua +default: wlan.lua fs.lua init.lua nl.lua svc.lua net/constants.lua test: ln -s . anoia diff --git a/pkgs/anoia/default.nix b/pkgs/anoia/default.nix index b981f48..3488e66 100644 --- a/pkgs/anoia/default.nix +++ b/pkgs/anoia/default.nix @@ -4,6 +4,7 @@ , linotify , lua , lualinux +, iwinfo , cpio }: let pname = "anoia"; @@ -12,7 +13,7 @@ in stdenv.mkDerivation { version = "0.1"; src = ./.; nativeBuildInputs = [ fennel cpio ]; - buildInputs = with lua.pkgs; [ linotify lualinux ]; + buildInputs = with lua.pkgs; [ linotify lualinux iwinfo ]; outputs = [ "out" "dev" ]; doCheck = true; diff --git a/pkgs/anoia/wlan.fnl b/pkgs/anoia/wlan.fnl new file mode 100644 index 0000000..39eeeb6 --- /dev/null +++ b/pkgs/anoia/wlan.fnl @@ -0,0 +1,8 @@ +(local { : nl80211 } (require :iwinfo)) + +(fn is-bridgeable [ifname] + (let [mode (nl80211.mode ifname)] + (or (= mode "Master") (= mode "Master (VLAN)")) +)) + +{ : is-bridgeable } diff --git a/pkgs/ifwait/default.nix b/pkgs/ifwait/default.nix index 19f76d0..b99eb54 100644 --- a/pkgs/ifwait/default.nix +++ b/pkgs/ifwait/default.nix @@ -1,11 +1,14 @@ { lua , netlink-lua +, lualinux +, iwinfo , writeFennelScript , runCommand , anoia }: runCommand "ifwait" {} '' mkdir -p $out/bin - cp -p ${writeFennelScript "ifwait" [anoia netlink-lua] ./ifwait.fnl} $out/bin/ifwait + cp -p ${writeFennelScript "ifwait" [ anoia netlink-lua ] ./ifwait.fnl} $out/bin/ifwait + cp -p ${writeFennelScript "ifbridgeable" [ anoia lualinux iwinfo ] ./ifbridgeable.fnl} $out/bin/ifbridgeable '' diff --git a/pkgs/ifwait/ifbridgeable.fnl b/pkgs/ifwait/ifbridgeable.fnl new file mode 100644 index 0000000..3ddfd4b --- /dev/null +++ b/pkgs/ifwait/ifbridgeable.fnl @@ -0,0 +1,30 @@ +(local wlan (require :anoia.wlan)) +(local { : assoc } (require :anoia)) +(local { : msleep } (require :lualinux)) + +(fn parse-args [args] + (match args + ["-v" & rest] (assoc (parse-args rest) :verbose true) + [linkname] {:link linkname} + _ nil)) + +(fn run [args poll-fn] + (let [parameters + (assert (parse-args args) + (.. "Usage: ifbridgeable [-v] ifname"))] + (when parameters.verbose + (print (.. "ifbridgeable: waiting for " + parameters.link " to be bridgeable"))) + + (while (not (poll-fn parameters.link)) + (when parameters.verbose + (print (.. "ifbridgeable: waiting for " parameters.link " to be bridgeable"))) + (msleep 500) + ) + ) +) + +(when (not (= (. arg 0) "test")) + (run arg wlan.is-bridgeable)) + +{ : run } -- 2.47.0 From 13069415fd5cc69ab71432b1bc588b150d13a500 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 21:12:36 +0200 Subject: [PATCH 38/57] feat(modules/hostapd): offer readiness oneshot WLAN oper wait until the WLAN interface is ready. Signed-off-by: Raito Bezarius --- modules/hostapd/default.nix | 11 +++++++++++ modules/hostapd/ready.nix | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 modules/hostapd/ready.nix diff --git a/modules/hostapd/default.nix b/modules/hostapd/default.nix index 4338e74..67f4f72 100644 --- a/modules/hostapd/default.nix +++ b/modules/hostapd/default.nix @@ -20,6 +20,10 @@ in { system.service.hostapd = mkOption { type = liminix.lib.types.serviceDefn; }; + + system.service.hostapd-ready = mkOption { + type = liminix.lib.types.serviceDefn; + }; }; config = { system.service.hostapd = liminix.callService ./service.nix { @@ -34,5 +38,12 @@ in { type = types.attrs; }; }; + + system.service.hostapd-ready = liminix.callService ./ready.nix { + interface = mkOption { + type = liminix.lib.types.interface; + description = "Interface for which to wait that the oper state Master or Master (VLAN) has been reached."; + }; + }; }; } diff --git a/modules/hostapd/ready.nix b/modules/hostapd/ready.nix new file mode 100644 index 0000000..7c10c32 --- /dev/null +++ b/modules/hostapd/ready.nix @@ -0,0 +1,16 @@ +{ + liminix +, ifwait +, lib +}: +{ interface } : +let + inherit (liminix.services) oneshot; +in oneshot { + name = "${interface.name}.wlan-oper"; + up = '' + ${ifwait}/bin/ifbridgeable -v $(output ${interface} ifname) + ''; + + dependencies = [ interface ]; +} -- 2.47.0 From 94a5b19c77c8c5806b1cd3b030ffb9a024e5344a Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 21:52:06 +0200 Subject: [PATCH 39/57] fix(evalConfig): fix `pkgs` relation with `nixpkgs` Signed-off-by: Raito Bezarius --- lib/eval-config.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/eval-config.nix b/lib/eval-config.nix index 7befab1..4c3c009 100644 --- a/lib/eval-config.nix +++ b/lib/eval-config.nix @@ -1,4 +1,4 @@ -{ nixpkgs ? , pkgs ? (import {}), lib ? pkgs.lib }: +{ nixpkgs ? , pkgs ? (import nixpkgs {}), lib ? pkgs.lib }: args: let modulesPath = builtins.toString ../modules; -- 2.47.0 From 0ee2ce41830b4c1f4beb4edbcd4f60f335fee6a1 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 21:52:30 +0200 Subject: [PATCH 40/57] fix(overlay): adopt upstream fix for Lua readline https://github.com/telent/liminix/commit/9f58e7b926652dcdbea58694831f7dea58b882c6 ("maybe fix nixpkgs-unstable lua") Signed-off-by: Raito Bezarius --- overlay.nix | 33 ++++++++++++++++----------------- pkgs/run-liminix-vm/default.nix | 7 ++++--- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/overlay.nix b/overlay.nix index 8ecfa5e..dda4ddc 100644 --- a/overlay.nix +++ b/overlay.nix @@ -1,23 +1,24 @@ final: prev: let + isCross = final.stdenv.buildPlatform != final.stdenv.hostPlatform; + crossOnly = pkg : amendFn : if isCross then (amendFn pkg) else pkg; extraPkgs = import ./pkgs/default.nix { inherit (final) lib callPackage; }; inherit (final) fetchpatch; - lua_no_readline = prev.lua5_3; -# lua_no_readline = prev.lua5_3.overrideAttrs(o: { -# name = "lua-tty"; -# preBuild = '' -# makeFlagsArray+=(PLAT="posix" SYSLIBS="-Wl,-E -ldl" CFLAGS="-O2 -fPIC -DLUA_USE_POSIX -DLUA_USE_DLOPEN") -# ''; -# # lua in nixpkgs has a postInstall stanza that assumes only -# # one output, we need to override that if we're going to -# # convert to multi-output -# # outputs = ["bin" "man" "out"]; -# makeFlags = -# builtins.filter (x: (builtins.match "(PLAT|MYLIBS).*" x) == null) -# o.makeFlags; -# }); + luaHost = prev.lua5_3.overrideAttrs(o: { + name = "lua-tty"; + preBuild = '' + makeFlagsArray+=(PLAT="posix" SYSLIBS="-Wl,-E -ldl" CFLAGS="-O2 -fPIC -DLUA_USE_POSIX -DLUA_USE_DLOPEN") + ''; + # lua in nixpkgs has a postInstall stanza that assumes only + # one output, we need to override that if we're going to + # convert to multi-output + # outputs = ["bin" "man" "out"]; + makeFlags = + builtins.filter (x: (builtins.match "(PLAT|MYLIBS).*" x) == null) + o.makeFlags; + }); s6 = prev.s6.overrideAttrs(o: let @@ -42,7 +43,6 @@ let (if o ? patches then o.patches else []) ++ (if patch_needed then [ patch ] else []); }); - lua = let s = lua_no_readline.override { self = s; }; in s; in extraPkgs // { # liminix library functions @@ -214,8 +214,7 @@ extraPkgs // { ]; }); - luaFull = prev.lua; - inherit lua; + lua = crossOnly prev.lua5_3 (_: luaHost); mtdutils = prev.mtdutils.overrideAttrs(o: { patches = (if o ? patches then o.patches else []) ++ [ diff --git a/pkgs/run-liminix-vm/default.nix b/pkgs/run-liminix-vm/default.nix index 1bf0389..f3df1e1 100644 --- a/pkgs/run-liminix-vm/default.nix +++ b/pkgs/run-liminix-vm/default.nix @@ -2,14 +2,15 @@ qemuLim , socat , writeShellScript -, writeFennel , runCommand +, fennel , lib , lua , pkgsBuildBuild }: let - run-liminix-vm = pkgsBuildBuild.writeFennel "run-liminix-vm" { - packages = [ qemuLim pkgsBuildBuild.lua.pkgs.luaposix pkgsBuildBuild.lua.pkgs.fennel ]; + writeFennel = pkgsBuildBuild.writeFennel.override { inherit lua; }; + run-liminix-vm = writeFennel "run-liminix-vm" { + packages = [ qemuLim lua.pkgs.luaposix fennel ]; } ./run-liminix-vm.fnl; connect = writeShellScript "connect-vm" '' export PATH="${lib.makeBinPath [socat]}:$PATH" -- 2.47.0 From 95850a44c2fd911c2a7f86b82b8ad577e68a31f4 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 22:28:05 +0200 Subject: [PATCH 41/57] feat(recovery): implement failsafe boot for TFTP or anything, really. Signed-off-by: Raito Bezarius --- modules/outputs/initramfs.nix | 25 +++++++++++++++++++++++-- pkgs/preinit/preinit.c | 10 ++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/modules/outputs/initramfs.nix b/modules/outputs/initramfs.nix index 89d017f..c666c77 100644 --- a/modules/outputs/initramfs.nix +++ b/modules/outputs/initramfs.nix @@ -41,17 +41,38 @@ in system.outputs = { initramfs = - let inherit (pkgs.pkgsBuildBuild) gen_init_cpio; + let + inherit (pkgs.pkgsBuildBuild) gen_init_cpio cpio writeScript; + inherit (pkgs) busybox; + failsafe-init = writeScript "init" '' + #!/bin/sh + exec >/dev/console + echo Running in initramfs + PATH=${busybox}/bin:$PATH + export PATH + mount -t proc none /proc + mount -t sysfs none /sys + ${busybox}/bin/sh + ''; + refs = pkgs.writeReferencesToFile busybox; in runCommand "initramfs.cpio" {} '' - cat << SPECIALS | ${gen_init_cpio}/bin/gen_init_cpio /dev/stdin > $out + cat << SPECIALS | ${gen_init_cpio}/bin/gen_init_cpio /dev/stdin > out dir /proc 0755 0 0 dir /dev 0755 0 0 nod /dev/console 0600 0 0 c 5 1 dir /target 0755 0 0 dir /target/persist 0755 0 0 dir /target/nix 0755 0 0 + dir /nix 0755 0 0 + dir /nix/store 0755 0 0 + dir /bin 0755 0 0 + file /bin/sh ${busybox}/bin/sh 0755 0 0 file /init ${pkgs.preinit}/bin/preinit 0755 0 0 + file /failsafe-init ${failsafe-init} 0755 0 0 SPECIALS + + find $(cat ${refs}) | ${cpio}/bin/cpio -H newc -o -A -v -O out + mv out $out ''; systemConfiguration = pkgs.systemconfig config.filesystem.contents; diff --git a/pkgs/preinit/preinit.c b/pkgs/preinit/preinit.c index 7dd896b..4b5f3f1 100644 --- a/pkgs/preinit/preinit.c +++ b/pkgs/preinit/preinit.c @@ -98,6 +98,7 @@ int main(int argc, char *argv[], char *envp[]) AVER(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts)); } else { if(mount(opts.device, "/target/persist", opts.fstype, 0, opts.mount_opts) < 0) { + ERR("failed to mount primary device, mount the alternative device\n"); AVER(mount(opts.altdevice, "/target/persist", opts.fstype, 0, opts.mount_opts)); } } @@ -118,5 +119,14 @@ int main(int argc, char *argv[], char *envp[]) AVER(execve("/persist/init", argv, envp)); } + + ERR("failed to mount the rootfs\n"); + ERR("final stand using the failsafe initialization method\n"); + ERR("the boot process is manual from now on\n"); + + argv[0] = "init"; + argv[1] = NULL; + AVER(execve("/failsafe-init", argv, envp)); + die(); } -- 2.47.0 From c942b2be09f3113411f8d07cbd63c7df9795602c Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 22:30:51 +0200 Subject: [PATCH 42/57] feat(initramfs): enable zstd compression Busybox is still 3MB. Signed-off-by: Raito Bezarius --- modules/outputs/initramfs.nix | 2 +- pkgs/kernel/default.nix | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/outputs/initramfs.nix b/modules/outputs/initramfs.nix index c666c77..b1e0ea8 100644 --- a/modules/outputs/initramfs.nix +++ b/modules/outputs/initramfs.nix @@ -36,7 +36,7 @@ in kernel.config = { BLK_DEV_INITRD = "y"; INITRAMFS_SOURCE = builtins.toJSON "${config.system.outputs.initramfs}"; -# INITRAMFS_COMPRESSION_LZO = "y"; + INITRAMFS_COMPRESSION_ZSTD = "y"; }; system.outputs = { diff --git a/pkgs/kernel/default.nix b/pkgs/kernel/default.nix index 4ddf1dd..6891384 100644 --- a/pkgs/kernel/default.nix +++ b/pkgs/kernel/default.nix @@ -23,7 +23,7 @@ stdenv.mkDerivation rec { nativeBuildInputs = [buildPackages.stdenv.cc] ++ (with buildPackages.pkgs; [ rsync bc bison flex pkg-config - openssl ncurses.all perl + openssl ncurses.all perl zstd ]); CC = "${stdenv.cc.bintools.targetPrefix}gcc"; HOSTCC = with buildPackages.pkgs; -- 2.47.0 From 73ea02b982fc68d69759ec193219000fd1279caa Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 23:05:03 +0200 Subject: [PATCH 43/57] feat(modules/nixpkgs): introduce source parameter Signed-off-by: Raito Bezarius --- modules/nixpkgs.nix | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/nixpkgs.nix b/modules/nixpkgs.nix index 15c9c0c..206d506 100644 --- a/modules/nixpkgs.nix +++ b/modules/nixpkgs.nix @@ -83,11 +83,11 @@ let localSystem = cfg.hostPlatform; }; in - import ({ + import cfg.source ({ inherit (cfg) config overlays; } // systemArgs) else - import { + import cfg.source { inherit (cfg) config overlays localSystem crossSystem; }; @@ -97,6 +97,14 @@ in { options.nixpkgs = { + source = mkOption { + type = types.package // { + description = "Source of a nixpkgs repository"; + }; + + default = ; + defaultText = ""; + }; pkgs = mkOption { defaultText = literalExpression '' -- 2.47.0 From 9fcfae3eae0e3343d50ef711567cfc233254a0cd Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 23:28:50 +0200 Subject: [PATCH 44/57] feat(recovery): strengthen debugging capabilities of preinit Signed-off-by: Raito Bezarius --- pkgs/preinit/preinit.c | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/pkgs/preinit/preinit.c b/pkgs/preinit/preinit.c index 4b5f3f1..c9a1821 100644 --- a/pkgs/preinit/preinit.c +++ b/pkgs/preinit/preinit.c @@ -4,9 +4,13 @@ #include #include #include +#include #include #include #include +#include + +#include #include /* for COMMAND_LINE_SIZE */ @@ -44,6 +48,25 @@ static int fork_exec(char * command, char *args[]) return execve(command, args, NULL); } +static void debug_listdir(const char * path) +{ + DIR *mydir; + struct dirent *myfile; + struct stat mystat; + + char buf[512]; + mydir = opendir(path); + while((myfile = readdir(mydir)) != NULL) + { + sprintf(buf, "%s/%s", path, myfile->d_name); + stat(buf, &mystat); + printf("%llu", mystat.st_size); + printf(" %s\n", myfile->d_name); + } + closedir(mydir); + +} + char banner[] = "Running pre-init...\n"; char buf[COMMAND_LINE_SIZE]; @@ -108,7 +131,12 @@ int main(int argc, char *argv[], char *envp[]) "bind", MS_BIND, NULL)); char *exec_args[] = { "activate", "/target", NULL }; - AVER(fork_exec("/target/persist/activate", exec_args)); + if (fork_exec("/target/persist/activate", exec_args) < 0) { + ERR("failed to activate the system\n"); + pr_u32(errno); ERR ( " - "); ERR(strerror(errno)); ERR("\n"); + goto failsafe; + } + AVER(chdir("/target")); AVER(mount("/target", "/", "bind", MS_BIND | MS_REC, NULL)); @@ -120,13 +148,22 @@ int main(int argc, char *argv[], char *envp[]) AVER(execve("/persist/init", argv, envp)); } +failsafe: + debug_listdir("/"); + debug_listdir("/target"); + ERR("failed to mount the rootfs\n"); ERR("final stand using the failsafe initialization method\n"); ERR("the boot process is manual from now on\n"); argv[0] = "init"; argv[1] = NULL; + // Attempt to unmount the /target mount-bind. + AVER(umount("/target")); AVER(execve("/failsafe-init", argv, envp)); + debug_listdir("/"); + debug_listdir("/target"); + die(); } -- 2.47.0 From 3ed1564235fd109941eb7ffb5e0b882a6bfa0a6b Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 23:38:18 +0200 Subject: [PATCH 45/57] feat(recovery): bump to 20MB the TFTP phram size Otherwise, this will fail to boot on a modern closure. Signed-off-by: Raito Bezarius --- devices/zyxel-nwa50ax/default.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/devices/zyxel-nwa50ax/default.nix b/devices/zyxel-nwa50ax/default.nix index 393dcac..a7834dc 100644 --- a/devices/zyxel-nwa50ax/default.nix +++ b/devices/zyxel-nwa50ax/default.nix @@ -222,8 +222,8 @@ imageFormat = "fit"; tftp = { - # 5MB is nice. - freeSpaceBytes = 5 * 1024 * 1024; + # 20MB is pretty good on this device as we have plenty of RAM. + freeSpaceBytes = 20 * 1024 * 1024; appendDTB = true; loadAddress = lim.parseInt "0x2000000"; }; -- 2.47.0 From b468275f53bcb427e337413ace62bfa465e8020d Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 23:48:13 +0200 Subject: [PATCH 46/57] fix(tftpboot): revert squashfs use and go back to JFFS2 squashfs doesn't copy all the files we need. Signed-off-by: Raito Bezarius --- modules/outputs/tftpboot.nix | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/modules/outputs/tftpboot.nix b/modules/outputs/tftpboot.nix index ca51d10..a8fc4dc 100644 --- a/modules/outputs/tftpboot.nix +++ b/modules/outputs/tftpboot.nix @@ -12,10 +12,15 @@ let arch = pkgs.stdenv.hostPlatform.linuxArch; # UBI cannot run on the top of phram. - needsSquashfs = config.rootfsType == "ubifs"; - rootfstype = if needsSquashfs then "squashfs" else config.rootfsType; - rootfs = if needsSquashfs then - liminix.builders.squashfs config.filesystem.contents + needsJffs2 = config.rootfsType == "ubifs"; + # squashfs doesn't work out for us because only `bootablerootdir` + # contain what we need to boot, not `config.filesystem.contents` alas. + rootfstype = if needsJffs2 then "jffs2" else config.rootfsType; + rootfs = if needsJffs2 then + liminix.builders.jffs2 { + bootableRootDirectory = config.system.outputs.bootablerootdir; + inherit (config.hardware.flash) eraseBlockSize; + } else config.system.outputs.rootfs; in { imports = [ ../ramdisk.nix ]; @@ -82,9 +87,13 @@ in { config = { boot.ramdisk.enable = true; - kernel.config = mkIf needsSquashfs { - SQUASHFS = "y"; - SQUASHFS_XZ = "y"; + kernel.config = mkIf needsJffs2 { + JFFS2_FS = "y"; + JFFS2_LZO = "y"; + JFFS2_RTIME = "y"; + JFFS2_COMPRESSION_OPTIONS = "y"; + JFFS2_ZLIB = "y"; + JFFS2_CMODE_SIZE = "y"; }; system.outputs = rec { -- 2.47.0 From 6970d811e87a3a99a093705cb089a5a63961cfe7 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sat, 7 Sep 2024 23:59:29 +0200 Subject: [PATCH 47/57] chore(min-copy-closure): improve liminix-rebuild for maintenance Signed-off-by: Raito Bezarius --- pkgs/min-copy-closure/liminix-rebuild.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pkgs/min-copy-closure/liminix-rebuild.sh b/pkgs/min-copy-closure/liminix-rebuild.sh index 16ec7f1..18f57f0 100755 --- a/pkgs/min-copy-closure/liminix-rebuild.sh +++ b/pkgs/min-copy-closure/liminix-rebuild.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +set -Eeuo pipefail ssh_command=${SSH_COMMAND-ssh} @@ -13,19 +14,24 @@ case "$1" in reboot="soft" shift ;; + "--root") + root_prefix="$2" + shift + shift + ;; esac target_host=$1 shift if [ -z "$target_host" ] ; then - echo Usage: liminix-rebuild \[--no-reboot\] target-host params + echo Usage: liminix-rebuild \[--no-reboot\] \[--fast\] target-host params exit 1 fi if toplevel=$(nix-build "$@" -A outputs.systemConfiguration --no-out-link); then echo systemConfiguration $toplevel - min-copy-closure $target_host $toplevel + min-copy-closure --root "$root_prefix" $target_host $toplevel $ssh_command $target_host $toplevel/bin/install case "$reboot" in reboot) -- 2.47.0 From 8ac3e32b8b5503ed6c16ac9e81f3016c723f0c21 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sun, 8 Sep 2024 00:31:15 +0200 Subject: [PATCH 48/57] fix(modules/hostname): hash the hostname to avoid duplicate services in the db Signed-off-by: Raito Bezarius --- modules/hostname.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/hostname.nix b/modules/hostname.nix index d5cd962..5f18b04 100644 --- a/modules/hostname.nix +++ b/modules/hostname.nix @@ -15,7 +15,7 @@ in { }; config = { services.hostname = oneshot { - name = "hostname"; + name = "hostname-${builtins.substring 0 12 (builtins.hashString "sha256" config.hostname)}"; up = "echo ${config.hostname} > /proc/sys/kernel/hostname"; down = "true"; }; -- 2.47.0 From 54db2ad0061ffba046a678ba6a4e9683fbc22873 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sun, 8 Sep 2024 01:08:54 +0200 Subject: [PATCH 49/57] feat(libubox): support for Lua 5.3 Signed-off-by: Raito Bezarius --- pkgs/libubox/default.nix | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkgs/libubox/default.nix b/pkgs/libubox/default.nix index 43b0377..3111ff2 100644 --- a/pkgs/libubox/default.nix +++ b/pkgs/libubox/default.nix @@ -1,9 +1,9 @@ { lib, stdenv, - fetchgit, + fetchFromGitea, cmake, - lua5_1, + lua, json_c }: @@ -11,30 +11,32 @@ stdenv.mkDerivation rec { pname = "libubox"; version = "unstable-2024-04-09"; - src = fetchgit { - url = "https://git.openwrt.org/project/libubox.git"; - rev = "eb9bcb64185ac155c02cc1a604692c4b00368324"; - hash = "sha256-5KO2E+4pcDp/pe2+vjoQDmyMwCc0yKm847U4J6HjxyA="; + src = fetchFromGitea { + domain = "git.dgnum.eu"; + owner = "DGNum"; + repo = "libubox"; + rev = "1c4b2dc4c12848e1b70b11e1cb2139ca8f19c860"; + hash = "sha256-aPhGJ7viXQmnoQRY8DuRvtwtxSy+S4qPj1fBsK066Yc="; }; nativeBuildInputs = [ cmake - lua5_1 + lua ]; buildInputs = [ - lua5_1 + lua json_c ]; # Otherwise, CMake cannot find jsoncpp? - env.NIX_CFLAGS_COMPILE = toString [ "-I${json_c.dev}/include/json-c" "-D JSONC" ]; + env.NIX_CFLAGS_COMPILE = toString [ "-I${json_c.dev}/include/json-c" "-D JSONC" "-D LUA_COMPAT_MODULE" ]; cmakeFlags = [ "-DBUILD_EXAMPLES=off" # TODO: it explode at install phase. "-DBUILD_LUA=on" - "-DLUAPATH=${placeholder "out"}/lib/lua" + "-DLUAPATH=${placeholder "out"}/lib/lua/${lua.luaversion}/" ]; meta = { -- 2.47.0 From 036f91d2f0990404dba2bb643da7642ad7f5673e Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Sun, 8 Sep 2024 01:08:59 +0200 Subject: [PATCH 50/57] feat(ubus): support for Lua 5.3 Signed-off-by: Raito Bezarius --- pkgs/ubus/default.nix | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pkgs/ubus/default.nix b/pkgs/ubus/default.nix index 88ea647..fa063bf 100644 --- a/pkgs/ubus/default.nix +++ b/pkgs/ubus/default.nix @@ -1,13 +1,14 @@ -{ stdenv, fetchFromGitHub, cmake, libubox, json_c, lua5_1, defaultSocketLocation ? "/run/ubus/ubus.sock" }: +{ stdenv, fetchFromGitea, lib, cmake, libubox, json_c, lua, defaultSocketLocation ? "/run/ubus/ubus.sock" }: stdenv.mkDerivation { pname = "ubus"; version = "unstable-04-09-2024"; - src = fetchFromGitHub { - owner = "openwrt"; + src = fetchFromGitea { + domain = "git.dgnum.eu"; + owner = "DGNum"; repo = "ubus"; - rev = "65bb027054def3b94a977229fd6ad62ddd32345b"; - hash = "sha256-n82Ub0IiuvWbnlDCoN+0hjo/1PbplEbc56kuOYMrHxQ="; + rev = "ebb1dc92e4985538a8e18b7e926264118138f281"; + hash = "sha256-fo4zleC9R6uzlcOJ/jQ0t0nSBHUAq/uqPVd9xJdkAM0="; }; # We don't use /var/run/ in Liminix by default. @@ -21,14 +22,14 @@ stdenv.mkDerivation { ]; buildInputs = [ - lua5_1 + lua libubox json_c ]; cmakeFlags = [ "-DBUILD_LUA=on" - "-DLUAPATH=${placeholder "out"}/lib/lua" + "-DLUAPATH=${placeholder "out"}/lib/lua/${lua.luaversion}" "-DBUILD_EXAMPLES=off" ]; } -- 2.47.0 From 013e4c396c97767beed3051c51a6eb6780397b8f Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Mon, 16 Sep 2024 18:14:39 +0200 Subject: [PATCH 51/57] feat: repair CI and cleanup cross-compilation mechanism Signed-off-by: Raito Bezarius --- ci.nix | 22 ++++------------- devices/qemu/default.nix | 5 +++- devices/zyxel-nwa50ax/default.nix | 4 ++- lon.lock | 15 +++++++++++ lon.nix | 41 +++++++++++++++++++++++++++++++ modules/base.nix | 11 +++++++++ modules/outputs/tftpboot.nix | 8 ------ 7 files changed, 79 insertions(+), 27 deletions(-) create mode 100644 lon.lock create mode 100644 lon.nix diff --git a/ci.nix b/ci.nix index ec345dd..07d26f1 100644 --- a/ci.nix +++ b/ci.nix @@ -1,21 +1,15 @@ { - nixpkgs -, unstable -, liminix + sources ? import ./lon.nix +, nixpkgs ? sources.nixpkgs +, unstable ? nixpkgs +, liminix ? ./. , ... }: let - inherit (builtins) map; - pkgs = (import nixpkgs {}); + pkgs = (import nixpkgs { }); borderVmConf = ./bordervm.conf-example.nix; inherit (pkgs.lib.attrsets) genAttrs; devices = [ - "gl-ar750" - "gl-mt300a" - "gl-mt300n-v2" "qemu" - "qemu-aarch64" - "qemu-armv7l" - "tp-archer-ax23" "zyxel-nwa50ax" ]; vanilla = ./vanilla-configuration.nix; @@ -44,12 +38,6 @@ let imports = [ ./modules/all-modules.nix ]; }; }).outputs.optionsJson; - installers = map (f: "system.outputs.${f}") [ - "vmroot" - "mtdimage" - "ubimage" - ]; - inherit (pkgs.lib) concatStringsSep; in pkgs.stdenv.mkDerivation { name = "liminix-doc"; nativeBuildInputs = with pkgs; [ diff --git a/devices/qemu/default.nix b/devices/qemu/default.nix index b6e860d..fe98952 100644 --- a/devices/qemu/default.nix +++ b/devices/qemu/default.nix @@ -1,7 +1,7 @@ # This "device" generates images that can be used with the QEMU # emulator. The default output is a directory containing separate # kernel (uncompressed vmlinux) and initrd (squashfs) images -{ +rec { system = { crossSystem = { config = "mips-unknown-linux-musl"; @@ -41,6 +41,9 @@ ../../modules/arch/mipseb.nix ../families/qemu.nix ]; + + nixpkgs.hostPlatform = system.crossSystem; + kernel = { config = { MIPS_MALTA= "y"; diff --git a/devices/zyxel-nwa50ax/default.nix b/devices/zyxel-nwa50ax/default.nix index a7834dc..2b4813d 100644 --- a/devices/zyxel-nwa50ax/default.nix +++ b/devices/zyxel-nwa50ax/default.nix @@ -1,4 +1,4 @@ -{ +rec { system = { crossSystem = { config = "mipsel-unknown-linux-musl"; @@ -135,6 +135,8 @@ ../../modules/zyxel-dual-image ]; + nixpkgs.hostPlatform = system.crossSystem; + filesystem = dir { lib = dir { firmware = dir { diff --git a/lon.lock b/lon.lock new file mode 100644 index 0000000..50209e4 --- /dev/null +++ b/lon.lock @@ -0,0 +1,15 @@ +{ + "version": "1", + "sources": { + "nixpkgs": { + "type": "GitHub", + "fetchType": "tarball", + "owner": "nixos", + "repo": "nixpkgs", + "branch": "nixos-unstable-small", + "revision": "b6227cadb5123c7e4cb159bf6f9f5705ae081a47", + "url": "https://github.com/nixos/nixpkgs/archive/b6227cadb5123c7e4cb159bf6f9f5705ae081a47.tar.gz", + "hash": "sha256-KFR30GNFhjzXl0kVEn+KK4xrFr0gggb1NBroP8ukbxY=" + } + } +} diff --git a/lon.nix b/lon.nix new file mode 100644 index 0000000..5f320ea --- /dev/null +++ b/lon.nix @@ -0,0 +1,41 @@ +# Generated by lon. Do not modify! +let + + lock = builtins.fromJSON (builtins.readFile ./lon.lock); + + # Override with a path defined in an environment variable. If no variable is + # set, the original path is used. + overrideFromEnv = + name: path: + let + replacement = builtins.getEnv "LON_OVERRIDE_${name}"; + in + if replacement == "" then + path + else + # this turns the string into an actual Nix path (for both absolute and + # relative paths) + if builtins.substring 0 1 replacement == "/" then + /. + replacement + else + /. + builtins.getEnv "PWD" + "/${replacement}"; + + fetchSource = + args@{ fetchType, ... }: + if fetchType == "git" then + builtins.fetchGit { + url = args.url; + ref = args.branch; + rev = args.revision; + narHash = args.hash; + } + else if fetchType == "tarball" then + builtins.fetchTarball { + url = args.url; + sha256 = args.hash; + } + else + builtins.throw "Unsupported source type ${fetchType}"; + +in +builtins.mapAttrs (name: args: overrideFromEnv name (fetchSource args)) lock.sources diff --git a/modules/base.nix b/modules/base.nix index d2182c6..41550e6 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -69,6 +69,14 @@ in { default = "uimage"; }; tftp = { + commandLine = mkOption { + type = types.listOf types.str; + default = config.boot.commandLine; + description = '' + TFTP-specific command line. + Defaults to the classical one if unset. + ''; + }; loadAddress = mkOption { type = types.ints.unsigned; description = '' @@ -98,6 +106,9 @@ in { }; }; config = { + # 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 ]; diff --git a/modules/outputs/tftpboot.nix b/modules/outputs/tftpboot.nix index a8fc4dc..4fd139d 100644 --- a/modules/outputs/tftpboot.nix +++ b/modules/outputs/tftpboot.nix @@ -41,14 +41,6 @@ in { type = types.bool; default = false; }; - commandLine = mkOption { - type = types.listOf types.str; - default = config.boot.commandLine; - description = '' - TFTP-specific command line. - Defaults to the classical one if unset. - ''; - }; }; options.system.outputs = { tftpboot = mkOption { -- 2.47.0 From 0dd72b31f4e7acb76f9b672214c253e92d4f1580 Mon Sep 17 00:00:00 2001 From: catvayor Date: Wed, 18 Sep 2024 09:10:07 +0200 Subject: [PATCH 52/57] feat(ci): use ci.nix --- .forgejo/workflows/build.yaml | 22 +++++++++++++++++++++- ci.nix | 4 ++-- default.nix | 9 ++++++--- tests/ext4/test.nix | 2 +- tests/fennel/test.nix | 2 +- tests/inout/test.nix | 2 +- tests/jffs2/configuration.nix | 1 - tests/jffs2/test.nix | 2 +- tests/min-copy-closure/configuration.nix | 1 - tests/min-copy-closure/test.nix | 2 +- tests/pppoe/test.nix | 2 +- tests/tftpboot/test.nix | 3 ++- tests/updown/test.nix | 2 +- tests/wlan/test.nix | 4 ++-- 14 files changed, 40 insertions(+), 18 deletions(-) diff --git a/.forgejo/workflows/build.yaml b/.forgejo/workflows/build.yaml index dcb2ee4..3a29607 100644 --- a/.forgejo/workflows/build.yaml +++ b/.forgejo/workflows/build.yaml @@ -17,4 +17,24 @@ jobs: - name: Build VM QEMU MIPS run: | # Enter the shell - nix-build -I liminix-config=./examples/hello-from-qemu.nix --arg device "import ./devices/qemu" -A outputs.default + nix-build ci.nix -A qemu + + build_zyxel-nwa50ax_mips: + runs-on: nix + steps: + - uses: actions/checkout@v3 + + - name: Build VM QEMU MIPS + run: | + # Enter the shell + nix-build ci.nix -A qemu + + test_hostapd: + runs-on: nix + steps: + - uses: actions/checkout@v3 + + - name: Build VM QEMU MIPS + run: | + # Enter the shell + nix-build ci.nix -A wlan diff --git a/ci.nix b/ci.nix index 07d26f1..73d8c97 100644 --- a/ci.nix +++ b/ci.nix @@ -7,7 +7,7 @@ let pkgs = (import nixpkgs { }); borderVmConf = ./bordervm.conf-example.nix; - inherit (pkgs.lib.attrsets) genAttrs; + inherit (pkgs.lib.attrsets) genAttrs mapAttrs; devices = [ "qemu" "zyxel-nwa50ax" @@ -19,7 +19,7 @@ let device = import (liminix + "/devices/${name}"); liminix-config = vanilla; }).outputs.default; - tests = import ./tests/ci.nix; + tests = mapAttrs (_: v: v { inherit liminix nixpkgs; }) (import ./tests/ci.nix); jobs = (genAttrs devices for-device) // tests // diff --git a/default.nix b/default.nix index e372095..33593dc 100644 --- a/default.nix +++ b/default.nix @@ -26,9 +26,12 @@ let eval = evalModules { modules = [ { - nixpkgs.overlays = [ - overlay - ]; + nixpkgs = { + overlays = [ overlay ]; + config.permittedInsecurePackages = [ + "python-2.7.18.8" + ]; + }; } device.module liminix-config diff --git a/tests/ext4/test.nix b/tests/ext4/test.nix index f972583..5c5a582 100644 --- a/tests/ext4/test.nix +++ b/tests/ext4/test.nix @@ -6,7 +6,7 @@ let img = (import liminix { device = import "${liminix}/devices/qemu/"; liminix-config = ./configuration.nix; }).outputs.vmroot; - pkgs = import { overlays = [(import ../../overlay.nix)]; }; + pkgs = import nixpkgs { overlays = [(import ../../overlay.nix)]; }; in pkgs.runCommand "check" { nativeBuildInputs = with pkgs; [ expect diff --git a/tests/fennel/test.nix b/tests/fennel/test.nix index fda2a89..81206d9 100644 --- a/tests/fennel/test.nix +++ b/tests/fennel/test.nix @@ -4,7 +4,7 @@ }: let overlay = import "${liminix}/overlay.nix"; - pkgs = import { overlays = [overlay]; }; + pkgs = import nixpkgs { overlays = [overlay]; }; script = pkgs.writeFennelScript "foo" [] ./hello.fnl; inherit (pkgs.lua.pkgs) fifo; netlink = pkgs.netlink-lua; diff --git a/tests/inout/test.nix b/tests/inout/test.nix index 5f382e4..3f3b198 100644 --- a/tests/inout/test.nix +++ b/tests/inout/test.nix @@ -6,7 +6,7 @@ let img = (import liminix { device = import "${liminix}/devices/qemu/"; liminix-config = ./configuration.nix; }).outputs.vmroot; - pkgs = import { overlays = [(import ../../overlay.nix)]; }; + pkgs = import nixpkgs { overlays = [(import ../../overlay.nix)]; }; in pkgs.runCommand "check" { nativeBuildInputs = with pkgs; [ expect diff --git a/tests/jffs2/configuration.nix b/tests/jffs2/configuration.nix index 2515fbf..ea7670b 100644 --- a/tests/jffs2/configuration.nix +++ b/tests/jffs2/configuration.nix @@ -5,7 +5,6 @@ in { imports = [ ../../vanilla-configuration.nix ../../modules/squashfs.nix - ../../modules/outputs/jffs2.nix ]; config.rootfsType = "jffs2"; config.filesystem = dir { diff --git a/tests/jffs2/test.nix b/tests/jffs2/test.nix index f972583..5c5a582 100644 --- a/tests/jffs2/test.nix +++ b/tests/jffs2/test.nix @@ -6,7 +6,7 @@ let img = (import liminix { device = import "${liminix}/devices/qemu/"; liminix-config = ./configuration.nix; }).outputs.vmroot; - pkgs = import { overlays = [(import ../../overlay.nix)]; }; + pkgs = import nixpkgs { overlays = [(import ../../overlay.nix)]; }; in pkgs.runCommand "check" { nativeBuildInputs = with pkgs; [ expect diff --git a/tests/min-copy-closure/configuration.nix b/tests/min-copy-closure/configuration.nix index fe80bf2..0797341 100644 --- a/tests/min-copy-closure/configuration.nix +++ b/tests/min-copy-closure/configuration.nix @@ -13,7 +13,6 @@ let in { imports = [ ../../vanilla-configuration.nix - ../../modules/outputs/jffs2.nix ]; config = { services.sshd = longrun { diff --git a/tests/min-copy-closure/test.nix b/tests/min-copy-closure/test.nix index 14f0225..774b882 100644 --- a/tests/min-copy-closure/test.nix +++ b/tests/min-copy-closure/test.nix @@ -8,7 +8,7 @@ let lmx = (import liminix { }); rogue = lmx.pkgs.rogue; img = lmx.outputs.vmroot; - pkgs = import { overlays = [(import ../../overlay.nix)]; }; + pkgs = import nixpkgs { overlays = [(import ../../overlay.nix)]; }; in pkgs.runCommand "check" { nativeBuildInputs = with pkgs; [ expect diff --git a/tests/pppoe/test.nix b/tests/pppoe/test.nix index c8007a9..6972256 100644 --- a/tests/pppoe/test.nix +++ b/tests/pppoe/test.nix @@ -6,7 +6,7 @@ let img = (import liminix { device = import "${liminix}/devices/qemu"; liminix-config = ./configuration.nix; }).outputs.default; - pkgs = import { overlays = [(import ../../overlay.nix)]; }; + pkgs = import nixpkgs { overlays = [(import ../../overlay.nix)]; }; inherit (pkgs.pkgsBuildBuild) routeros; in pkgs.runCommand "check" { nativeBuildInputs = with pkgs; [ diff --git a/tests/tftpboot/test.nix b/tests/tftpboot/test.nix index b7a3e87..b6b9f1a 100644 --- a/tests/tftpboot/test.nix +++ b/tests/tftpboot/test.nix @@ -1,5 +1,6 @@ { - liminix + liminix, + ... }: let check = deviceName : config : let derivation = (import liminix { diff --git a/tests/updown/test.nix b/tests/updown/test.nix index 589e383..45f7c91 100644 --- a/tests/updown/test.nix +++ b/tests/updown/test.nix @@ -6,7 +6,7 @@ let img = (import liminix { device = import "${liminix}/devices/qemu/"; liminix-config = ./configuration.nix; }).outputs.vmroot; - pkgs = import { overlays = [(import ../../overlay.nix)]; }; + pkgs = import nixpkgs { overlays = [(import ../../overlay.nix)]; }; in pkgs.runCommand "check" { nativeBuildInputs = with pkgs; [ expect diff --git a/tests/wlan/test.nix b/tests/wlan/test.nix index fcc2715..f644fb9 100644 --- a/tests/wlan/test.nix +++ b/tests/wlan/test.nix @@ -3,10 +3,10 @@ , nixpkgs }: let img = (import liminix { - device = import "${liminix}/devices/qemu-armv7l/"; + device = import "${liminix}/devices/qemu/"; liminix-config = ./configuration.nix; }).outputs.default; - pkgs = import { overlays = [(import ../../overlay.nix)]; }; + pkgs = import nixpkgs { overlays = [(import ../../overlay.nix)]; }; in pkgs.runCommand "check" { nativeBuildInputs = with pkgs; [ expect socat -- 2.47.0 From c5e48f5c9fb38c721e6d186019269324ff5662d7 Mon Sep 17 00:00:00 2001 From: catvayor Date: Wed, 18 Sep 2024 17:04:51 +0200 Subject: [PATCH 53/57] feat(ci-wlan): use wpa_supplicant --- default.nix | 1 + overlay.nix | 6 +++++- tests/wlan/configuration.nix | 19 ++++++++++++++++++- tests/wlan/test.nix | 1 + tests/wlan/wait-for-wlan.expect | 4 ++-- tests/wlan/wpa_service.nix | 21 +++++++++++++++++++++ tests/wlan/wpa_supplicant.nix | 15 +++++++++++++++ 7 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/wlan/wpa_service.nix create mode 100644 tests/wlan/wpa_supplicant.nix diff --git a/default.nix b/default.nix index 33593dc..fd1c357 100644 --- a/default.nix +++ b/default.nix @@ -27,6 +27,7 @@ let modules = [ { nixpkgs = { + source = nixpkgs; overlays = [ overlay ]; config.permittedInsecurePackages = [ "python-2.7.18.8" diff --git a/overlay.nix b/overlay.nix index dda4ddc..891e9a0 100644 --- a/overlay.nix +++ b/overlay.nix @@ -194,7 +194,11 @@ extraPkgs // { }); in h.override { openssl = null; sqlite = null; }; - + wpa_supplicant = prev.wpa_supplicant.override { + dbusSupport = false; + withPcsclite = false; + wpa_supplicant_gui = null; + }; kexec-tools-static = prev.kexec-tools.overrideAttrs(o: { # For kexecboot we copy kexec into a ramdisk on the system being diff --git a/tests/wlan/configuration.nix b/tests/wlan/configuration.nix index 6dc4b44..ed1d948 100644 --- a/tests/wlan/configuration.nix +++ b/tests/wlan/configuration.nix @@ -7,6 +7,7 @@ in rec { ../../modules/wlan.nix ../../modules/hostapd ../../modules/network + ./wpa_supplicant.nix ]; services.hostap = config.system.service.hostapd.build { @@ -27,5 +28,21 @@ in rec { }; }; - defaultProfile.packages = with pkgs; [ tcpdump ] ; + services.wpa_supplicant = config.system.service.wpa_supplicant.build { + interface = "wlan1"; + driver = "nl80211"; + config-file = pkgs.writeText "wpa_supplicant.conf" '' + country=us + update_config=1 + ctrl_interface=/run/wpa_supplicant + + network={ + scan_ssid=1 + ssid="liminix" + psk="colourless green ideas" + } + ''; + }; + + defaultProfile.packages = with pkgs; [ tcpdump wpa_supplicant ]; } diff --git a/tests/wlan/test.nix b/tests/wlan/test.nix index f644fb9..f1f20c2 100644 --- a/tests/wlan/test.nix +++ b/tests/wlan/test.nix @@ -3,6 +3,7 @@ , nixpkgs }: let img = (import liminix { + inherit nixpkgs; device = import "${liminix}/devices/qemu/"; liminix-config = ./configuration.nix; }).outputs.default; diff --git a/tests/wlan/wait-for-wlan.expect b/tests/wlan/wait-for-wlan.expect index 1d42a4e..5e0f073 100644 --- a/tests/wlan/wait-for-wlan.expect +++ b/tests/wlan/wait-for-wlan.expect @@ -14,10 +14,10 @@ expect { } expect "#" while { $FINISHED < 10 } { - send "date && grep AP-ENABLED /run/uncaught-logs/* || echo \$NOT\r\n" + send "date && grep CTRL-EVENT-CONNECTED /run/uncaught-logs/* || echo \$NOT\r\n" expect { - "wlan0: AP-ENABLED" { set FINISHED 999; set EXIT 0; } + "wlan1: CTRL-EVENT-CONNECTED" { set FINISHED 999; set EXIT 0; } "not_present" { send_user "waiting ...\n" ; sleep 5 } } set FINISHED [ expr $FINISHED + 1 ] diff --git a/tests/wlan/wpa_service.nix b/tests/wlan/wpa_service.nix new file mode 100644 index 0000000..ae70db3 --- /dev/null +++ b/tests/wlan/wpa_service.nix @@ -0,0 +1,21 @@ +{ + liminix, + wpa_supplicant, + lib, +}: +{ + interface, + driver, + config-file, +}: +let + inherit (liminix.services) longrun; + inherit (lib.strings) escapeShellArg; +in +longrun { + name = "wpa_supplicant"; + run = + '' + ${wpa_supplicant}/bin/wpa_supplicant -D${driver} -i${interface} -c ${config-file} + ''; +} diff --git a/tests/wlan/wpa_supplicant.nix b/tests/wlan/wpa_supplicant.nix new file mode 100644 index 0000000..98f7f08 --- /dev/null +++ b/tests/wlan/wpa_supplicant.nix @@ -0,0 +1,15 @@ +{ config, lib, pkgs, ... }: +with lib; { + options.system.service.wpa_supplicant = mkOption { type = pkgs.liminix.lib.types.serviceDefn; }; + config.system.service.wpa_supplicant = config.system.callService ./wpa_service.nix { + interface = mkOption { + type = types.str; + }; + driver = mkOption { + type = types.str; + }; + config-file = mkOption { + type = types.package; + }; + }; +} -- 2.47.0 From 562b05034136363989620ab65ecdc830b53db9f1 Mon Sep 17 00:00:00 2001 From: Raito Bezarius Date: Tue, 17 Sep 2024 16:35:33 +0200 Subject: [PATCH 54/57] feat: add environment variables and prompt customization in login shells This way, we can configure a bit our prompt. Signed-off-by: Raito Bezarius --- modules/base.nix | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/modules/base.nix b/modules/base.nix index 41550e6..0d507a7 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -4,11 +4,13 @@ { lib, pkgs, config, ...}: let - inherit (lib) mkEnableOption mkOption types isDerivation hasAttr ; + inherit (lib) mkEnableOption mkOption types isDerivation hasAttr concatStringsSep; inherit (pkgs.pseudofile) dir symlink; inherit (pkgs.liminix.networking) address interface; inherit (pkgs.liminix.services) bundle; + # TODO: escape shell argument. + exportVar = name: value: "export ${name}=\"${value}\""; type_service = pkgs.liminix.lib.types.service; in { @@ -22,6 +24,24 @@ in { /run/current-system, we just add the paths in /etc/profile ''; }; + + environmentVariables = mkOption { + type = types.attrsOf types.str; + description = '' + Attribute set of environment variables to make available + in a login shell. + + The value is assumed to be escaped and the name to be valid. + ''; + }; + + prompt = mkOption { + type = types.str; + default = "$(whoami)@$(hostname) # "; + description = '' + Prompt string (PS1) for the shell. + ''; + }; }; services = mkOption { type = types.attrsOf type_service; @@ -111,6 +131,8 @@ in { 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; boot.commandLine = [ "panic=10 oops=panic init=/bin/init loglevel=8" @@ -181,9 +203,10 @@ in { etc = let profile = symlink (pkgs.writeScript ".profile" '' - PATH=${lib.makeBinPath config.defaultProfile.packages}:/bin + PATH=${lib.makeBinPath config.defaultProfile.packages}:/bin export PATH - ''); + ${concatStringsSep "\n" (map exportVar config.defaultProfile.environmentVariables)} + ''); in dir { inherit profile; ashrc = profile; -- 2.47.0 From eec7a6e985424200678afe090922c89457f5e4b8 Mon Sep 17 00:00:00 2001 From: catvayor Date: Fri, 27 Sep 2024 10:17:34 +0200 Subject: [PATCH 55/57] fix PS1 --- modules/base.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/base.nix b/modules/base.nix index 0d507a7..5107e1e 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -4,7 +4,7 @@ { lib, pkgs, config, ...}: let - inherit (lib) mkEnableOption mkOption types isDerivation hasAttr concatStringsSep; + inherit (lib) mkEnableOption mkOption types isDerivation hasAttr concatStringsSep mapAttrsToList; inherit (pkgs.pseudofile) dir symlink; inherit (pkgs.liminix.networking) address interface; inherit (pkgs.liminix.services) bundle; @@ -205,7 +205,7 @@ in { (pkgs.writeScript ".profile" '' PATH=${lib.makeBinPath config.defaultProfile.packages}:/bin export PATH - ${concatStringsSep "\n" (map exportVar config.defaultProfile.environmentVariables)} + ${concatStringsSep "\n" (mapAttrsToList exportVar config.defaultProfile.environmentVariables)} ''); in dir { inherit profile; -- 2.47.0 From 89d2d34ad77646ec7f405863907067b19cc41168 Mon Sep 17 00:00:00 2001 From: catvayor Date: Fri, 27 Sep 2024 10:35:53 +0200 Subject: [PATCH 56/57] feat(ci): prompt checking --- .forgejo/workflows/build.yaml | 10 ++++++++++ tests/ci.nix | 1 + tests/custom-shell/check-prompt.expect | 7 +++++++ tests/custom-shell/configuration.nix | 13 +++++++++++++ tests/custom-shell/test.nix | 21 +++++++++++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 tests/custom-shell/check-prompt.expect create mode 100644 tests/custom-shell/configuration.nix create mode 100644 tests/custom-shell/test.nix diff --git a/.forgejo/workflows/build.yaml b/.forgejo/workflows/build.yaml index 3a29607..71964a9 100644 --- a/.forgejo/workflows/build.yaml +++ b/.forgejo/workflows/build.yaml @@ -38,3 +38,13 @@ jobs: run: | # Enter the shell nix-build ci.nix -A wlan + + test_shell_customization: + runs-on: nix + steps: + - uses: actions/checkout@v3 + + - name: Build VM QEMU MIPS + run: | + # Enter the shell + nix-build ci.nix -A custom-shell diff --git a/tests/ci.nix b/tests/ci.nix index 4fbd953..1334885 100644 --- a/tests/ci.nix +++ b/tests/ci.nix @@ -10,4 +10,5 @@ tftpboot = import ./tftpboot/test.nix; updown = import ./updown/test.nix; inout = import ./inout/test.nix; + custom-shell = import ./custom-shell/test.nix; } diff --git a/tests/custom-shell/check-prompt.expect b/tests/custom-shell/check-prompt.expect new file mode 100644 index 0000000..4cc7656 --- /dev/null +++ b/tests/custom-shell/check-prompt.expect @@ -0,0 +1,7 @@ +set timeout 60 + +spawn socat unix-connect:vm/console - +expect { + "root@liminix blah blah > " { exit 0 } + timeout { exit 1 } +} diff --git a/tests/custom-shell/configuration.nix b/tests/custom-shell/configuration.nix new file mode 100644 index 0000000..b908137 --- /dev/null +++ b/tests/custom-shell/configuration.nix @@ -0,0 +1,13 @@ +{ config, pkgs, lib, ... } : +let + inherit (pkgs.liminix.networking) interface address hostapd route dnsmasq; + inherit (pkgs.liminix.services) oneshot longrun bundle target; +in rec { + imports = [ + ../../modules/network + ]; + + defaultProfile.prompt = "$(whoami)@$(hostname) blah blah > "; + + defaultProfile.packages = with pkgs; [ ]; +} diff --git a/tests/custom-shell/test.nix b/tests/custom-shell/test.nix new file mode 100644 index 0000000..e5273e1 --- /dev/null +++ b/tests/custom-shell/test.nix @@ -0,0 +1,21 @@ +{ + liminix +, nixpkgs +}: +let img = (import liminix { + inherit nixpkgs; + device = import "${liminix}/devices/qemu/"; + liminix-config = ./configuration.nix; + }).outputs.default; + pkgs = import nixpkgs { overlays = [(import ../../overlay.nix)]; }; +in pkgs.runCommand "check" { + nativeBuildInputs = with pkgs; [ + expect socat + ] ; +} '' +. ${../test-helpers.sh} + +mkdir vm +${img}/run.sh --background ./vm +expect ${./check-prompt.expect} |tee output && mv output $out +'' -- 2.47.0 From 7eff028b022ec6e5ae210305196b86b999a03452 Mon Sep 17 00:00:00 2001 From: catvayor Date: Fri, 27 Sep 2024 16:18:13 +0200 Subject: [PATCH 57/57] fix: hostname at early boot --- modules/hostname.nix | 22 +++++++++++++++------- modules/s6/default.nix | 2 ++ modules/s6/scripts/rc.init | 1 + 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/modules/hostname.nix b/modules/hostname.nix index 5f18b04..e05b724 100644 --- a/modules/hostname.nix +++ b/modules/hostname.nix @@ -1,7 +1,6 @@ { lib, pkgs, config, ...}: let inherit (lib) mkOption types; - inherit (pkgs.liminix.services) oneshot; in { options = { hostname = mkOption { @@ -12,12 +11,21 @@ in { default = "liminix"; type = types.nonEmptyStr; }; - }; - config = { - services.hostname = oneshot { - name = "hostname-${builtins.substring 0 12 (builtins.hashString "sha256" config.hostname)}"; - up = "echo ${config.hostname} > /proc/sys/kernel/hostname"; - down = "true"; + hostname-script = mkOption { + description = '' + Script that outputs the system hostname on stdin. + ''; + default = pkgs.writeScript "hostname-gen" '' + #!/bin/sh + echo ${config.hostname} + ''; + defaultText = '' + pkgs.writeScript "hostname-gen" ''' + #!/bin/sh + echo ''${config.hostname} + ''' + ''; + type = types.package; }; }; } diff --git a/modules/s6/default.nix b/modules/s6/default.nix index cd05de7..e1ce9c8 100644 --- a/modules/s6/default.nix +++ b/modules/s6/default.nix @@ -30,6 +30,8 @@ let installPhase = '' mkdir $out cp -r $src $out/scripts + substituteInPlace $out/scripts/rc.init \ + --replace-fail 'config.hostname' "${config.hostname-script}" chmod -R +w $out ''; }; diff --git a/modules/s6/scripts/rc.init b/modules/s6/scripts/rc.init index c098ffb..3888e50 100755 --- a/modules/s6/scripts/rc.init +++ b/modules/s6/scripts/rc.init @@ -36,6 +36,7 @@ fi ### (replace /run/service with your scandir) s6-rc-init -d -c /etc/s6-rc/compiled /run/service +config.hostname > /proc/sys/kernel/hostname ### 2. Starting the wanted set of services ### This is also called every time you change runlevels with telinit. -- 2.47.0