From 51e8da2a74f918f460b05c938e010187fca0e347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 22 Sep 2023 14:29:25 +0200 Subject: [PATCH] config: get rid of impure eval --- nixosModules/clanCore/flake-module.nix | 1 + nixosModules/clanCore/options.nix | 12 +++++++ pkgs/clan-cli/clan_cli/config/__init__.py | 40 +++++++---------------- pkgs/clan-cli/tests/test_flake/flake.nix | 24 ++++++++++---- 4 files changed, 43 insertions(+), 34 deletions(-) create mode 100644 nixosModules/clanCore/options.nix diff --git a/nixosModules/clanCore/flake-module.nix b/nixosModules/clanCore/flake-module.nix index 0fcf4cd..0ea9291 100644 --- a/nixosModules/clanCore/flake-module.nix +++ b/nixosModules/clanCore/flake-module.nix @@ -8,6 +8,7 @@ # just some example options. Can be removed later ./bloatware ./vm.nix + ./options.nix ]; options.clanSchema = lib.mkOption { type = lib.types.attrs; diff --git a/nixosModules/clanCore/options.nix b/nixosModules/clanCore/options.nix new file mode 100644 index 0000000..b207799 --- /dev/null +++ b/nixosModules/clanCore/options.nix @@ -0,0 +1,12 @@ +{ pkgs, options, lib, ... }: { + options.clanCore.optionsNix = lib.mkOption { + type = lib.types.raw; + internal = true; + readOnly = true; + default = (pkgs.nixosOptionsDoc { inherit options; }).optionsNix; + defaultText = "optionsNix"; + description = '' + This is to export nixos options used for `clan config` + ''; + }; +} diff --git a/pkgs/clan-cli/clan_cli/config/__init__.py b/pkgs/clan-cli/clan_cli/config/__init__.py index c5c0004..dcb53c4 100644 --- a/pkgs/clan-cli/clan_cli/config/__init__.py +++ b/pkgs/clan-cli/clan_cli/config/__init__.py @@ -2,6 +2,7 @@ import argparse import json import os +import shlex import subprocess import sys from pathlib import Path @@ -95,42 +96,25 @@ def cast(value: Any, type: Type, opt_description: str) -> Any: ) -def options_for_machine(machine_name: str, flake: Optional[Path] = None) -> dict: - if flake is None: - flake = get_clan_flake_toplevel() +def options_for_machine(machine_name: str) -> dict: + clan_dir = get_clan_flake_toplevel() # use nix eval to lib.evalModules .#clanModules.machine-{machine_name} + cmd = nix_eval( + flags=[ + "--show-trace", + f"{clan_dir}#flake.nixosConfigurations.{machine_name}.config.clanCore.optionsNix", + ], + ) proc = subprocess.run( - nix_eval( - flags=[ - "--show-trace", - "--impure", - "--expr", - f""" - let - flake = builtins.getFlake (toString {flake}); - lib = flake.inputs.nixpkgs.lib; - options = flake.nixosConfigurations.{machine_name}.options; - - # this is actually system independent as it uses toFile - docs = flake.inputs.nixpkgs.legacyPackages.x86_64-linux.nixosOptionsDoc {{ - inherit options; - }}; - opts = builtins.fromJSON (builtins.readFile docs.optionsJSON.options); - in - opts - """, - ], - ), + cmd, capture_output=True, text=True, ) if proc.returncode != 0: - print(proc.stderr, file=sys.stderr) raise Exception( - f"Failed to read options for machine {machine_name}:\n{proc.stderr}" + f"Failed to read options for machine {machine_name}:\n{shlex.join(cmd)} returned:\n{proc.stderr}" ) - options = json.loads(proc.stdout) - return options + return json.loads(proc.stdout) def read_machine_option_value(machine_name: str, option: str) -> str: diff --git a/pkgs/clan-cli/tests/test_flake/flake.nix b/pkgs/clan-cli/tests/test_flake/flake.nix index 23d2a67..be233b9 100644 --- a/pkgs/clan-cli/tests/test_flake/flake.nix +++ b/pkgs/clan-cli/tests/test_flake/flake.nix @@ -9,12 +9,24 @@ (if builtins.pathExists ./machines/machine1/settings.json then builtins.fromJSON (builtins.readFile ./machines/machine1/settings.json) else { }) - { - nixpkgs.hostPlatform = "x86_64-linux"; - # speed up by not instantiating nixpkgs twice and disable documentation - nixpkgs.pkgs = inputs.nixpkgs.legacyPackages.x86_64-linux; - documentation.enable = false; - } + ({ lib, options, pkgs, ... }: { + config = { + nixpkgs.hostPlatform = "x86_64-linux"; + # speed up by not instantiating nixpkgs twice and disable documentation + nixpkgs.pkgs = inputs.nixpkgs.legacyPackages.x86_64-linux; + documentation.enable = false; + }; + options.clanCore.optionsNix = lib.mkOption { + type = lib.types.raw; + internal = true; + readOnly = true; + default = (pkgs.nixosOptionsDoc { inherit options; }).optionsNix; + defaultText = "optionsNix"; + description = '' + This is to export nixos options used for `clan config` + ''; + }; + }) ]; }; };