config: get rid of impure eval

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 # just some example options. Can be removed later
./bloatware ./bloatware
./vm.nix ./vm.nix
./options.nix
]; ];
options.clanSchema = lib.mkOption { options.clanSchema = lib.mkOption {
type = lib.types.attrs; 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 argparse
import json import json
import os import os
import shlex
import subprocess import subprocess
import sys import sys
from pathlib import Path 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: def options_for_machine(machine_name: str) -> dict:
if flake is None: clan_dir = get_clan_flake_toplevel()
flake = get_clan_flake_toplevel()
# use nix eval to lib.evalModules .#clanModules.machine-{machine_name} # use nix eval to lib.evalModules .#clanModules.machine-{machine_name}
proc = subprocess.run( cmd = nix_eval(
nix_eval(
flags=[ flags=[
"--show-trace", "--show-trace",
"--impure", f"{clan_dir}#flake.nixosConfigurations.{machine_name}.config.clanCore.optionsNix",
"--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
""",
], ],
), )
proc = subprocess.run(
cmd,
capture_output=True, capture_output=True,
text=True, text=True,
) )
if proc.returncode != 0: if proc.returncode != 0:
print(proc.stderr, file=sys.stderr)
raise Exception( 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 json.loads(proc.stdout)
return options
def read_machine_option_value(machine_name: str, option: str) -> str: def read_machine_option_value(machine_name: str, option: str) -> str:

View File

@@ -9,12 +9,24 @@
(if builtins.pathExists ./machines/machine1/settings.json (if builtins.pathExists ./machines/machine1/settings.json
then builtins.fromJSON (builtins.readFile ./machines/machine1/settings.json) then builtins.fromJSON (builtins.readFile ./machines/machine1/settings.json)
else { }) else { })
{ ({ lib, options, pkgs, ... }: {
config = {
nixpkgs.hostPlatform = "x86_64-linux"; nixpkgs.hostPlatform = "x86_64-linux";
# speed up by not instantiating nixpkgs twice and disable documentation # speed up by not instantiating nixpkgs twice and disable documentation
nixpkgs.pkgs = inputs.nixpkgs.legacyPackages.x86_64-linux; nixpkgs.pkgs = inputs.nixpkgs.legacyPackages.x86_64-linux;
documentation.enable = false; 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`
'';
};
})
]; ];
}; };
}; };