repositories

NixDaemon

NixOS pentest workstation as one flake — IceBreaker's successor

clonegit clone https://git.daemon-sec.xyz/NixDaemon.git
options.nixNix85 lines4.3 KiBraw download
# modules/home/_nixmod/options.nix — what nixmod asks the flake.
#
# Evaluated with `nix eval --impure --json --expr` against this repo's own
# nixosConfigurations.<host>, so the options are the ones home-manager and
# nixpkgs are pinned to in flake.lock, not whatever the website shows today.
# Nothing is built: reading `options` only evaluates declarations.
#
#   mode = "options"  the option docs for `want` (or the first of programs.<name>,
#                     services.<name> that exists, home-manager before NixOS)
#   mode = "pkg"      only the package's description, version and homepage
#   mode = "search"   option-module and package names containing `name`
{ repo, host, name, scope ? "auto", want ? null, mode ? "options" }:
let
  flake = builtins.getFlake repo;
  sys = flake.nixosConfigurations.${host};
  lib = flake.inputs.nixpkgs.lib;
  pkgs = sys.pkgs;

  home = sys.options.home-manager.users.type.getSubOptions [ ];
  trees = { home = home; nixos = sys.options; };
  scopes = if scope == "auto" then [ "home" "nixos" ] else [ scope ];

  safe = v: let r = builtins.tryEval (builtins.deepSeq v v); in if r.success then r.value else null;
  isOpt = lib.isOption;

  # [ scope [ "programs" "firefox" ] ] candidates, in priority order.
  candidates =
    if want != null then map (s: { scope = s; path = lib.splitString "." want; }) scopes
    else lib.concatMap (s: map (p: { scope = s; path = [ p name ]; }) [ "programs" "services" ]) scopes;
  found = lib.findFirst (c: lib.hasAttrByPath c.path trees.${c.scope}) null candidates;

  docs = c:
    let sub = lib.getAttrFromPath c.path trees.${c.scope};
        prefixLength = builtins.length c.path + (if c.scope == "home" then 1 else 0);
    in if isOpt sub then [ ]
    else builtins.filter (o: o != null) (map (o:
      let r = builtins.tryEval (builtins.deepSeq o.name (!(o.internal or false) && o.visible == true && !o.readOnly));
          original = lib.attrByPath (lib.drop prefixLength (lib.splitString "." o.name)) { } sub;
          input = if c.scope == "home" then flake.inputs.home-manager else flake.inputs.nixpkgs;
          sourceUrl = file:
            let root = toString input.outPath + "/";
            in if lib.hasPrefix root (toString file) && input ? rev then
              "https://github.com/${if c.scope == "home" then "nix-community/home-manager" else "NixOS/nixpkgs"}/blob/${input.rev}/${lib.removePrefix root (toString file)}"
            else null;
      in if r.success && r.value then {
        inherit (o) name;
        type = safe (o.type or null);
        description = safe (o.description or null);
        default = safe (o.default or null);
        example = safe (o.example or null);
        # DocList renders examples into Nix text. Keep the original JSON-safe
        # value too, so nixmod can put editable keys inside their real wrapper.
        exampleValue = safe (builtins.fromJSON (builtins.toJSON (original.example or null)));
        sources = builtins.filter (url: url != null) (map sourceUrl (original.declarations or [ ]));
      } else null) (lib.optionAttrSetToDocList sub));

  pkg =
    let
      p = builtins.tryEval (pkgs.${name} or null);
      d = if p.success && p.value != null && lib.isDerivation p.value then p.value else null;
    in if d == null then null else {
      description = safe (d.meta.description or null);
      version = safe (d.version or null);
      homepage = safe (d.meta.homepage or null);
    };

  match = n: lib.hasInfix (lib.toLower name) (lib.toLower n);
  listing = s: kind: map (n: "${s}:${kind}.${n}") (builtins.filter match
    (builtins.attrNames (builtins.removeAttrs trees.${s}.${kind} [ "_module" ])));
in
if mode == "pkg" then { inherit pkg; }
else if mode == "search" then {
  modules = lib.concatMap (s: listing s "programs" ++ listing s "services") [ "home" "nixos" ];
  packages = lib.take 40 (builtins.filter match (builtins.attrNames pkgs));
}
else {
  inherit pkg;
  scope = if found == null then null else found.scope;
  path = if found == null then null else lib.concatStringsSep "." found.path;
  options = if found == null then [ ] else docs found;
  # The name home-manager prefixes every option with (`<name>` for
  # users.<name>), so nixmod can strip it.
  prefix = if found == null then "" else
    (if found.scope == "home" then "<name>." else "") + lib.concatStringsSep "." found.path;
}