Getting options of a module for nixd autocompletion

I’ve recently started writing my own modules for nixos configuration to share them between different machines. So I wanted to have nice auto completion for my own options via nixd.

But I’m struggling to understand where do I get options definitions from. The guide provided by nixd seems to be using nix-parts (based on the attributes in used flakes, e. g. (builtins.getFlake \"/home/lyc/flakes\").nixosConfigurations.adrastea.options). The only non-flake based way they suggest is (import <home-manager/modules> { configuration = ~/.config/home-manager/home.nix; pkgs = import <nixpkgs> {}; }).options, but it is specific for home-manager.

I’ve also tried to evaluate modules myself, with pkgs.lib.evalModules. But I haven’t figured it out how to actually use it on my modules.

test.nix

{lib, ...}:{
  options.testOption = lib.mkEnableOption "test";
}

more-complex.nix

{ config, pkgs, lib, ... }:
{
  options = ...;
}

nix repl

nix-repl> (pkgs.lib.evalModules {modules = [ (import /path/to/file/test.nix) ];})
{
  _module = { ... };
  _type = "configuration";
  class = null;
  config = { ... };
  extendModules = «lambda extendModules @ /nix/store/ag2xcw8qdzji8plkd9fishqd2b95wzrd-nixos-24.11/nixos/lib/modules.nix:338:9»;
  options = { ... };
  type = { ... };
}

nix-repl> (pkgs.lib.evalModules {modules = [ (import /path/to/file/more-complex.nix) ];})
{
  _module = «error: attribute 'pkgs' missing»;
  _type = "configuration";
  class = null;
  config = «error: attribute 'pkgs' missing»;
  extendModules = «lambda extendModules @ /nix/store/ag2xcw8qdzji8plkd9fishqd2b95wzrd-nixos-24.11/nixos/lib/modules.nix:338:9»;
  options = «error: attribute 'pkgs' missing»;
  type = { ... };
}

It breaks if pkgs is passed to it. I don’t know how to pass pkgs to this module and there hardly any documentation on this function.

So, can someone explain how do you get these config definitions from nested modules?

Just to clarify, you aren’t using flakes?

Yes. At least not now.

I added the non-nix docs, but it only covers hm, because I don’t do anything custom with nixos :slight_smile:

However I still think the basic pattern would be the same. Assuming your custom modules don’t live in glorious isolation from nixos, then what you want to is to eval nixos using your root configuration. You can figure this out in two ways.

  1. Know that nixos-rebuild repl somehow is doing what you want
  2. Know that building <nixpkgs/nixos> does something useful.

Lets assume mystically you’ve decided to look at nixpkgs/nixos/default.nix you’ll notice it takes an argument which is the path to a specific configuration.

❯ nix repl --file '<nixpkgs>'
Nix 2.24.14
Type :? for help.
Loading installable ''...
Added 23037 variables.
nix-repl> options = (import <nixpkgs/nixos> { configuration = /etc/nixos/configuration.nix; }).options

nix-repl> options.
options._module            options.ec2                options.jobs               options.nixpkgs            options.security           options.time
options.appstream          options.environment        options.krb5               options.oci                options.services           options.users
options.assertions         options.fileSystems        options.lib                options.openstack          options.snapraid           options.virtualisation
options.boot               options.fonts              options.location           options.passthru           options.sound              options.warnings
options.console            options.gtk                options.meta               options.power              options.specialisation     options.xdg
options.containers         options.hardware           options.nesting            options.powerManagement    options.stubby             options.zramSwap
options.docker-containers  options.i18n               options.networking         options.programs           options.swapDevices
options.documentation      options.ids                options.nix                options.qt                 options.system
options.dysnomia           options.isSpecialisation   options.nixops             options.qt5                options.systemd

These options will contain any custom options you imported under your configuration.

You could test this out then a contribution to the nixd docs :slight_smile:

1 Like

It works.
Thank you very much!