Managed to solve my previous flake issues. What is left now are just tests. Before the rewrite I did
something like export NIXOS_CONFIG
and then invoked nixos = import <nixpkgs/nixos> {}
and did some assertions on nixos.config
- like that there are all necessary users present etc. Now I wonder how to “port” this to flakes (since there is no /etc/nixos/configuration.nix
anymore). What I’d like basically is inside nix code obtain modules config
(for current computer). Thanks.
Again answering myself but oh well at least I learned something new. Seems I totally missed there is builtins.getFlake
.
So now I do something like:
test.nix
{ hostname ? "default", pkgs ? import <nixpkgs> { } }:
let
flake = builtins.getFlake (toString /etc/nixos);
lib = pkgs.lib;
users = flake.nixosConfigurations.${hostname}.config.users.users;
normalUsers = builtins.filter (x: x.isNormalUser) (lib.toList (lib.attrValues users));
num = toString (builtins.length normalUsers);
in
pkgs.mkShell {
shellHook = ''
echo "${num}"
exit 0
'';
}
And to invoke it I do:
nix-shell test.nix --arg hostname \"$(hostname)\"
Of course this is just an approximation, I have more test cases and if they fail I return exit code != 0.
Open for suggestions how to do this in a more elegant fashion, possibly through nix
command.
1 Like
You might consider implementing these as checks inside your flake itself! Then you can do nix flake check
and get your “tests” run for free. I think you’d put the check in the “checks” attribute.
This one is testing/exercising a Rust program, but you should be able to do the same general strategy you’ve shown above in the flake directly. A Flake for your Crate