From 887a4ff9661e4bde7d113c4b104804f7be8fb009 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 25 Sep 2022 11:10:22 +0100 Subject: [PATCH] WIP introduce nixos module system for configuration --- merge-modules.nix | 8 ++++++++ tests/module/configuration.nix | 10 ++++++++++ tests/module/defs.nix | 16 ++++++++++++++++ tests/module/module.nix | 6 ++++++ tests/module/run.sh | 2 ++ tests/module/test.nix | 17 +++++++++++++++++ 6 files changed, 59 insertions(+) create mode 100644 merge-modules.nix create mode 100644 tests/module/configuration.nix create mode 100644 tests/module/defs.nix create mode 100644 tests/module/module.nix create mode 100755 tests/module/run.sh create mode 100644 tests/module/test.nix diff --git a/merge-modules.nix b/merge-modules.nix new file mode 100644 index 0000000..d4fef55 --- /dev/null +++ b/merge-modules.nix @@ -0,0 +1,8 @@ +module : initial : pkgs : +let evalModules = (import ).evalModules; +in evalModules { + modules = [ + { _module.args = { inherit pkgs; }; } + module + ]; +} diff --git a/tests/module/configuration.nix b/tests/module/configuration.nix new file mode 100644 index 0000000..eef2a8a --- /dev/null +++ b/tests/module/configuration.nix @@ -0,0 +1,10 @@ +{ config, pkgs, ... } : +{ + imports = [ ./defs.nix ./module.nix ]; + config = { + services.a.enable = true; + services.b.enable = true; + + systemPackages = [ pkgs.hello ] ; + }; +} diff --git a/tests/module/defs.nix b/tests/module/defs.nix new file mode 100644 index 0000000..5ca3e29 --- /dev/null +++ b/tests/module/defs.nix @@ -0,0 +1,16 @@ +{ lib, ...}: +let inherit (lib) mkEnableOption mkOption types; +in { + options = { + services.a = { + enable = mkEnableOption "hello service"; + }; + services.b = { + enable = mkEnableOption "other service"; + }; + services.z = mkOption { }; + systemPackages = mkOption { + type = types.listOf types.package; + }; + }; +} diff --git a/tests/module/module.nix b/tests/module/module.nix new file mode 100644 index 0000000..69b4817 --- /dev/null +++ b/tests/module/module.nix @@ -0,0 +1,6 @@ +{ config, pkgs, ... } : +{ + services.z = pkgs.figlet; + + systemPackages = [ pkgs.units ] ; +} diff --git a/tests/module/run.sh b/tests/module/run.sh new file mode 100755 index 0000000..1197da8 --- /dev/null +++ b/tests/module/run.sh @@ -0,0 +1,2 @@ +set -e +NIXPKGS_ALLOW_UNSUPPORTED_SYSTEM=1 nix-build --arg device "import " test.nix diff --git a/tests/module/test.nix b/tests/module/test.nix new file mode 100644 index 0000000..a5adb20 --- /dev/null +++ b/tests/module/test.nix @@ -0,0 +1,17 @@ +{ device }: +let + overlay = import ; + nixpkgs = import ( device.system // {overlays = [overlay]; }); + inherit (nixpkgs) lib pkgs; + inherit (lib.asserts) assertMsg; + config = + (import ) ./configuration.nix {} pkgs; + res1 = assertMsg + # check we have packages from both modules + (config.config.systemPackages == ( with pkgs; [ units hello ])) "failed"; + res2 = let s = config.config.services; + in assertMsg (s.a.enable && s.b.enable && (s.z != null) ) "failed"; +in pkgs.writeText "foo" '' + ${if res1 then "OK" else "not OK"} + ${if res2 then "OK" else "not OK"} +''