From 83fe58e00318dc1f036843c17edfcf81a9540db9 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Fri, 31 May 2024 17:22:38 +0200 Subject: [PATCH] clan-core: add clan meta for ui usage --- flake.nix | 6 ++- flakeModules/clan.nix | 59 +++++++++++++++++++---- lib/build-clan/default.nix | 61 ++++++++++++++++++++++-- pkgs/clan-cli/tests/test_create_flake.py | 30 ++++++++++++ pkgs/installer/flake-module.nix | 3 -- templates/empty/.clan-flake | 2 + templates/empty/clan/meta.json | 5 ++ templates/empty/flake.nix | 16 +++++++ templates/flake-module.nix | 4 ++ 9 files changed, 167 insertions(+), 19 deletions(-) create mode 100644 templates/empty/.clan-flake create mode 100644 templates/empty/clan/meta.json create mode 100644 templates/empty/flake.nix diff --git a/flake.nix b/flake.nix index 3403db97..7b3f3ed9 100644 --- a/flake.nix +++ b/flake.nix @@ -24,10 +24,14 @@ }; outputs = - inputs@{ flake-parts, ... }: + inputs@{ flake-parts, self, ... }: flake-parts.lib.mkFlake { inherit inputs; } ( { ... }: { + clan = { + # meta.name = "clan-core"; + directory = self; + }; systems = [ "x86_64-linux" "aarch64-linux" diff --git a/flakeModules/clan.nix b/flakeModules/clan.nix index c53826cc..139ef8d9 100644 --- a/flakeModules/clan.nix +++ b/flakeModules/clan.nix @@ -17,6 +17,33 @@ let cfg = config.clan; in { + imports = [ + # TODO: figure out how to print the deprecation warning + # "${inputs.nixpkgs}/nixos/modules/misc/assertions.nix" + (lib.mkRenamedOptionModule + [ + "clan" + "clanName" + ] + [ + "clan" + "meta" + "name" + ] + ) + (lib.mkRenamedOptionModule + [ + "clan" + "clanIcon" + ] + [ + "clan" + "meta" + "icon" + ] + ) + ]; + options.clan = { directory = mkOption { type = types.path; @@ -33,15 +60,27 @@ in default = { }; description = "Allows to include machine-specific modules i.e. machines.\${name} = { ... }"; }; - clanName = mkOption { - type = types.str; - description = "Needs to be (globally) unique, as this determines the folder name where the flake gets downloaded to."; - }; - clanIcon = mkOption { - type = types.nullOr types.path; - default = null; - description = "A path to an icon to be used for the clan, should be the same for all machines"; + + # Checks are performed in 'buildClan' + # Not everyone uses flake-parts + meta = { + name = lib.mkOption { + type = types.nullOr types.str; + default = null; + description = "Needs to be (globally) unique, as this determines the folder name where the flake gets downloaded to."; + }; + icon = mkOption { + type = types.nullOr types.path; + default = null; + description = "A path to an icon to be used for the clan in the GUI"; + }; + description = mkOption { + type = types.nullOr types.str; + default = null; + description = "A short description of the clan"; + }; }; + pkgsForSystem = mkOption { type = types.functionTo types.raw; default = _system: null; @@ -52,6 +91,7 @@ in clanInternals = lib.mkOption { type = lib.types.submodule { options = { + meta = lib.mkOption { type = lib.types.attrsOf lib.types.unspecified; }; all-machines-json = lib.mkOption { type = lib.types.attrsOf lib.types.unspecified; }; machines = lib.mkOption { type = lib.types.attrsOf (lib.types.attrsOf lib.types.unspecified); }; machinesFunc = lib.mkOption { type = lib.types.attrsOf (lib.types.attrsOf lib.types.unspecified); }; @@ -65,9 +105,8 @@ in directory specialArgs machines - clanName - clanIcon pkgsForSystem + meta ; }; }; diff --git a/lib/build-clan/default.nix b/lib/build-clan/default.nix index ec5e6e1a..dda026c2 100644 --- a/lib/build-clan/default.nix +++ b/lib/build-clan/default.nix @@ -7,16 +7,58 @@ directory, # The directory containing the machines subdirectory specialArgs ? { }, # Extra arguments to pass to nixosSystem i.e. useful to make self available machines ? { }, # allows to include machine-specific modules i.e. machines.${name} = { ... } - clanName, # Needs to be (globally) unique, as this determines the folder name where the flake gets downloaded to. + # DEPRECATED: use meta.name instead + clanName ? null, # Needs to be (globally) unique, as this determines the folder name where the flake gets downloaded to. + # DEPRECATED: use meta.icon instead clanIcon ? null, # A path to an icon to be used for the clan, should be the same for all machines + meta ? { }, # A set containing clan meta: name :: string, icon :: string, description :: string pkgsForSystem ? (_system: null), # A map from arch to pkgs, if specified this nixpkgs will be only imported once for each system. # This improves performance, but all nipxkgs.* options will be ignored. }: let + deprecationWarnings = [ + (lib.warnIf ( + clanName != null + ) "clanName is deprecated, please use meta.name instead. ${clanName}" null) + (lib.warnIf (clanIcon != null) "clanIcon is deprecated, please use meta.icon instead" null) + ]; + machinesDirs = lib.optionalAttrs (builtins.pathExists "${directory}/machines") ( builtins.readDir (directory + /machines) ); + mergedMeta = + let + metaFromFile = + if (builtins.pathExists "${directory}/clan/meta.json") then + let + settings = builtins.fromJSON (builtins.readFile "${directory}/clan/meta.json"); + in + settings + else + { }; + legacyMeta = lib.filterAttrs (_: v: v != null) { + name = clanName; + icon = clanIcon; + }; + optionsMeta = lib.filterAttrs (_: v: v != null) meta; + + warnings = + builtins.map ( + name: + if + metaFromFile.${name} or null != optionsMeta.${name} or null && optionsMeta.${name} or null != null + then + lib.warn "meta.${name} is set in different places. (exlicit option meta.${name} overrides ${directory}/clan/meta.json)" null + else + null + ) (builtins.attrNames metaFromFile) + ++ [ (if (res.name or null == null) then (throw "meta.name should be set") else null) ]; + res = metaFromFile // legacyMeta // optionsMeta; + in + # Print out warnings before returning the merged result + builtins.deepSeq warnings res; + machineSettings = machineName: # CLAN_MACHINE_SETTINGS_FILE allows to override the settings file temporarily @@ -58,11 +100,15 @@ let (machines.${name} or { }) ( { - networking.hostName = lib.mkDefault name; - clanCore.clanName = clanName; - clanCore.clanIcon = clanIcon; + # Settings clanCore.clanDir = directory; + # Inherited from clan wide settings + clanCore.clanName = meta.name or clanName; + clanCore.clanIcon = meta.icon or clanIcon; + + # Machine specific settings clanCore.machineName = name; + networking.hostName = lib.mkDefault name; nixpkgs.hostPlatform = lib.mkDefault system; # speeds up nix commands by using the nixpkgs from the host system (especially useful in VMs) @@ -127,10 +173,15 @@ let ) supportedSystems ); in -{ +builtins.deepSeq deprecationWarnings { inherit nixosConfigurations; clanInternals = { + # Evaluated clan meta + # Merged /clan/meta.json with overrides from buildClan + meta = mergedMeta; + + # machine specifics machines = configsPerSystem; machinesFunc = configsFuncPerSystem; all-machines-json = lib.mapAttrs ( diff --git a/pkgs/clan-cli/tests/test_create_flake.py b/pkgs/clan-cli/tests/test_create_flake.py index 2fa7a88a..a58d2f59 100644 --- a/pkgs/clan-cli/tests/test_create_flake.py +++ b/pkgs/clan-cli/tests/test_create_flake.py @@ -47,3 +47,33 @@ def test_create_flake( flake_outputs["nixosConfigurations"]["machine1"] except KeyError: pytest.fail("nixosConfigurations.machine1 not found in flake outputs") + + +@pytest.mark.impure +def test_ui_template( + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture, + temporary_home: Path, + cli: Cli, +) -> None: + flake_dir = temporary_home / "test-flake" + url = "git+https://git.clan.lol/clan/clan-core#empty" + cli.run(["flakes", "create", str(flake_dir), "--url", url]) + assert (flake_dir / ".clan-flake").exists() + monkeypatch.chdir(flake_dir) + cli.run(["machines", "create", "machine1"]) + capsys.readouterr() # flush cache + + cli.run(["machines", "list"]) + assert "machine1" in capsys.readouterr().out + flake_show = subprocess.run( + ["nix", "flake", "show", "--json"], + check=True, + capture_output=True, + text=True, + ) + flake_outputs = json.loads(flake_show.stdout) + try: + flake_outputs["nixosConfigurations"]["machine1"] + except KeyError: + pytest.fail("nixosConfigurations.machine1 not found in flake outputs") diff --git a/pkgs/installer/flake-module.nix b/pkgs/installer/flake-module.nix index 59af6c60..f6243289 100644 --- a/pkgs/installer/flake-module.nix +++ b/pkgs/installer/flake-module.nix @@ -98,9 +98,6 @@ let in { clan = { - clanName = "clan-core"; - directory = self; - # To build a generic installer image (without ssh pubkeys), # use the following command: # $ nix build .#iso-installer diff --git a/templates/empty/.clan-flake b/templates/empty/.clan-flake new file mode 100644 index 00000000..406fcfeb --- /dev/null +++ b/templates/empty/.clan-flake @@ -0,0 +1,2 @@ +# DO NOT DELETE +# This file is used by the clan cli to discover a clan flake diff --git a/templates/empty/clan/meta.json b/templates/empty/clan/meta.json new file mode 100644 index 00000000..26fbf95e --- /dev/null +++ b/templates/empty/clan/meta.json @@ -0,0 +1,5 @@ +{ + "name": "My Empty Clan", + "description": "some nice description", + "icon": "A path to the png" +} diff --git a/templates/empty/flake.nix b/templates/empty/flake.nix new file mode 100644 index 00000000..689983f2 --- /dev/null +++ b/templates/empty/flake.nix @@ -0,0 +1,16 @@ +# Clan configuration file +# TODO: This file is used as a template for the simple GUI workflow +{ + inputs.clan-core.url = "git+file:///home/johannes/git/clan-core"; + outputs = + { self, clan-core, ... }: + let + clan = clan-core.lib.buildClan { + # This clan builds all its configuration out of the current directory + directory = self; + }; + in + { + inherit (clan) nixosConfigurations clanInternals; + }; +} diff --git a/templates/flake-module.nix b/templates/flake-module.nix index b8fe8e9f..4f5c842e 100644 --- a/templates/flake-module.nix +++ b/templates/flake-module.nix @@ -5,6 +5,10 @@ description = "Initialize a new clan flake"; path = ./new-clan; }; + empty = { + description = "A empty clan template. Primarily for usage with the clan ui"; + path = ./empty; + }; default = self.templates.new-clan; }; }