How to list enabled services in a NixOS test node?

I’m trying to expand on another post to get a working way to list all the enabled services in a NixOS test node. I can get to a useful REPL place for exploring a NixOS deployment by running nix repl .#.nixosConfigurations.NAME. How do I get to the same place in a .#.checks.ARCH.NAME.nodes.NAME derivation? For example, I can get the options for a NixOS configuration, but I can’t find the same attribute under .#.checks.ARCH.NAME.nodes.NAME. Based on the findings so far I’ve ended up with this horrible hack to work around all the ways in which filtering services can fail:

let
  nixosConfiguration = nixosConfigurations.ci-full-stable;
in
nixosConfiguration.pkgs.lib.filterAttrs (
    serviceName: serviceConfig: let
        serviceOptions = nixosConfiguration.options.services;
        isDefined = let result = builtins.tryEval (serviceOptions ? ${serviceName} && serviceOptions.${serviceName} ? enable && serviceOptions.${serviceName}.enable.isDefined); in result.success && result.value;
        isEnabled = let result = builtins.tryEval serviceConfig.enable; in result.success && result.value;
    in serviceName != "frp" && isDefined && isEnabled
) checks.x86_64-linux.androidBackupService.nodes.machine.services

The goal is to minimize the complexity and size of the test VM, and thereby maximize the chance that it runs reliably in CI.

There might be a nicer way to do this (at least it’s better than tryEval!), but something like this should suffice — I’m eyeballing this based on your example because I’m not fluent in flakes, but the equivalent of this works on the various attributes in nixosTests.

let
  extendedWithOptions = checks.x86_64-linux.androidBackupService.extendNixOS {
    module = { options, ... }: { passthru = { inherit options; }; };
  };
  predicate = name: svc: name != "frp" && svc.enable.isDefined or false && svc.enable.value;
in
filterAttrs predicate extendedWithOptions.nodes.machine.passthru.options.services
1 Like

Thank you! Ended up inlining everything and putting it in repl-history for reference.