Skip to content

Commit

Permalink
Merge pull request #42 from numtide/testing
Browse files Browse the repository at this point in the history
Do basic evaluation tests
  • Loading branch information
Mic92 committed Sep 29, 2024
2 parents 0bea32e + f8bd05e commit 9614462
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 96 deletions.
23 changes: 15 additions & 8 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,7 @@
flake = publicInputs.self;
}).value;
};
nixosModules = {
boot = ./modules/nixos/boot.nix;
facter = ./modules/nixos/facter.nix;
firmware = ./modules/nixos/firmware.nix;
networking = ./modules/nixos/networking;
system = ./modules/nixos/system.nix;
virtualisation = ./modules/nixos/virtualisation.nix;
};
nixosModules.facter = ./modules/nixos/facter.nix;
}
//
# DevOutputs
Expand All @@ -69,6 +62,20 @@
checks = eachSystem (
{ pkgs, ... }:
{
minimal-machine =
(pkgs.nixos [
publicInputs.self.nixosModules.facter
(
{ lib, config, ... }:
{
boot.loader.grub.devices = lib.mkForce [ "/dev/sda" ];
fileSystems."/".device = lib.mkDefault "/dev/sda";
users.users.root.initialPassword = "fnord23";
system.stateVersion = config.system.nixos.version;
nixpkgs.pkgs = pkgs;
}
)
]).config.system.build.toplevel;
lib-tests = pkgs.runCommandLocal "lib-tests" { nativeBuildInputs = [ pkgs.nix-unit ]; } ''
export HOME="$(realpath .)"
export NIX_CONFIG='
Expand Down
86 changes: 3 additions & 83 deletions modules/nixos/facter.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{
lib,
config,
options,
...
}:
let
cfg = config.facter;
modulePath = lib.concatStringsSep "/" (lib.take 4 (lib.splitString [ "/" ] __curPos.file));
in
{
imports = [
./bluetooth.nix
Expand All @@ -23,89 +18,14 @@ in
options.facter = with lib; {
report = mkOption {
type = types.raw;
default = builtins.fromJSON (builtins.readFile config.facter.reportPath);
default = if config.facter.reportPath == null then {} else builtins.fromJSON (builtins.readFile config.facter.reportPath);
description = "An import for the reportPath.";
};

reportPath = mkOption {
type = types.path;
type = types.nullOr types.path;
default = null;
description = "Path to a report generated by nixos-facter.";
};

debug = {
options = mkOption {
type = types.raw;
description = "All of the options affected by Facter modules";
};
config = mkOption {
type = types.raw;
description = "A breakdown of the NixOS config being applied by each Facter module.";
};
};
};

config.facter.debug = {
options =
let
# we want all options except our own, otherwise we get into recursive issues
otherOptions = lib.filterAttrs (n: _: n != "facter") options;

# a filter for identifying options where a Facter module has affected the value
touchedByFacter =
{ definitionsWithLocations, ... }:
let
# some options fail when we try to evaluate them, so we wrap this in tryEval
eval = builtins.tryEval (
builtins.any (
{
file ? "",
...
}:
# we only want options affected by our modules
lib.hasPrefix "${modulePath}/modules/nixos" file
) definitionsWithLocations
);
in
eval.success && eval.value;
in
lib.fold (a: b: lib.recursiveUpdate a b) { } (
map (value@{ loc, ... }: lib.setAttrByPath loc value) (
# we collect the options first with simple option filter, and then we filter them some more, otherwise we get
# a max-call depth exceeded error (dunno why)
lib.filter touchedByFacter (lib.collect lib.isOption otherOptions)
)
);

config =
# extract the config values for each option, broken down by facter module
lib.mapAttrsRecursiveCond
(
{
definitionsWithLocations ? null,
...
}:
# keep recursing if we are not processing an option, otherwise apply the map function
definitionsWithLocations == null
)
(
_:
{ definitionsWithLocations, ... }:
builtins.listToAttrs (
map
(
{ file, value }:
{
name = "<facter>${lib.removePrefix modulePath file}";
inherit value;
}
)
(
# we only want facter modules
lib.filter ({ file, ... }: lib.hasPrefix modulePath file) definitionsWithLocations
)
)
)
cfg.debug.options;
};

}
10 changes: 5 additions & 5 deletions modules/nixos/virtualisation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ in
defaultText = "hardware dependent";
};
oracle.enable = lib.mkEnableOption "Enable the Facter Virtualisation Oracle module" // {
default = report.virtualisation == "oracle";
default = report.virtualisation or null == "oracle";
defaultText = "environment dependent";
};
parallels.enable = lib.mkEnableOption "Enable the Facter Virtualisation Parallels module" // {
default = report.virtualisation == "parallels";
default = report.virtualisation or null == "parallels";
defaultText = "environment dependent";
};
qemu.enable = lib.mkEnableOption "Enable the Facter Virtualisation Qemu module" // {
default = builtins.elem report.virtualisation [
default = builtins.elem (report.virtualisation or null) [
"qemu"
"kvm"
"bochs"
];
defaultText = "environment dependent";
};
hyperv.enable = lib.mkEnableOption "Enable the Facter Virtualisation Hyper-V module" // {
default = report.virtualisation == "microsoft";
default = report.virtualisation or null == "microsoft";
defaultText = "environment dependent";
};
# no virtualisation detected
none.enable = lib.mkEnableOption "Enable the Facter Virtualisation None module" // {
default = report.virtualisation == "none";
default = report.virtualisation or null == "none";
defaultText = "environment dependent";
};
};
Expand Down

0 comments on commit 9614462

Please sign in to comment.