config: get rid of impure eval
All checks were successful
checks-impure / test (pull_request) Successful in 8s
checks / test (pull_request) Successful in 23s

This commit is contained in:
Jörg Thalheim 2023-09-22 14:29:25 +02:00
parent 56cd9d1cf4
commit 51e8da2a74
4 changed files with 43 additions and 34 deletions

View File

@ -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;

View File

@ -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`
'';
};
}

View File

@ -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:

View File

@ -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`
'';
};
})
];
};
};