How to import module from NixosConfiguration into repl

Hi,

How can I import a module from my nixos configuration into nix repl?

I have the module

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

with lib;
let devCfg = config.modules.dev;
    cfg = devCfg.python;
in {
  options.modules.dev.python = with types; {
    enable = mkEnableOption "python";
    xdg.enable = mkOption {
      type = bool;
      default = devCfg.xdg.enable;
    };
  };

  config = mkMerge [
    (mkIf cfg.enable {
      user.packages = with pkgs; [
        python3
      ];
      environment.shellAliases = {
        py = python3;
      };
    })

    (mkIf cfg.xdg.enable {
      env.PYTHONSTARTUP   = "$XDG_CONFIG_HOME/python/pythonrc";
    })
  ];
}

In nix repl I can load my systems flake and call the module function. But how can I easily give the attribute set when calling/loading the module, eg. something like

:lf /etc/nixos
# added 20 variables, like my libs, inputs,  ...
py = nixosModules.dev.python { inherit config, options, lib, pkgs;}
#or
py = import /etc/nixos/modules/dev/python.nix  { inherit config options lib pkgs;}

:p py.config.contents

But for this to work I need to do some tweaking

mylib = lib
lib = mylib.extend  (_: _:  inputs.nixpkgs.lib )
pkgs = inputs.nixpkgs.legacyPackages.x86_64-linux
config = nixosConfigurations.HOST.config
options = nixosConfigurations.HOST.options

Is there a better way to peek into a module from my configuration?

Addendum:
I want to do this for leaning and easier debugging. Lets say I have

modules/dev/test.nix

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

with lib;
let cfg = config.modules.dev;
in {
  options.modules.dev = {
    test.enable = mkOption { default = true; };
  };

  config = mkIf cfg.test.enable {
    b = trace (3+3) true;
  };
}

I would like to see the output of the trace.

You really ought to instantiate a nixosSystem with the module to try to use it. And it looks like you already have nixosConfigurations.HOST, so assuming that imports the module, it’s already there within nixosConfigurations.HOST.config. e.g. in that instance, since the module defines environment.shellAliases.py, you can do nixosConfigurations.HOST.config.environment.shellAliases.py

Your syntax is wrong btw. Function calling binds tighter than operators, so trace 3+3 true is actually (trace 3) + (3 true), which incorrectly treats trace as a function of only one argument, and also tries to call 3 as though it were a function instead of a number. Should be trace (3+3) true

Ok, thanks for the reminder wrt syntax.

It just seems slow to instantiate compared to loading and printing the module in the repl? In this case I want to inspect some paths.
I ended up doing

nix repl --file repl.nix
:p test.config

with repl.nix

let nixos = builtins.getFlake "/etc/nixos";
in rec {
  inherit nixos;
  inherit (nixos) inputs nixosConfigurations;
  mylib = nixos.lib;
  lib = mylib.extend  (_: _:  inputs.nixpkgs.lib );
  pkgs = inputs.nixpkgs.legacyPackages.x86_64-linux;
  config = nixosConfigurations.HOST.config;
  options = nixosConfigurations.HOST.options;
  test = nixos.nixosModules.dev.test {inherit lib pkgs config options;};
}

Whenever I change modules/dev/test.nix, I reload and print in the repl

:r
:p test

This mhu.dev has an idea that seems nice

tl;dr
mymodule.nix

{ lib, config, ... }:

let
  cfg = config.mymodule;
in
{
  options = {
    mymodule = {
      firstName = lib.mkOption {
        description = "Your first name";
        type = lib.types.str;
        default = "John";
      };
      lastName = lib.mkOption {
        description = "Your last name";
        type = lib.types.str;
        default = "Doe";
      };
      fullName = lib.mkOption {
        type = lib.types.str;
      };
    };
  };
  
  config = {
    mymodule.fullName = "${cfg.firstName} ${cfg.lastName}";
  };
}

sandbox.nix

(import <nixpkgs/lib>).evalModules {
  modules = [{
    # import your module here
    imports = [ ./mymodule.nix ];

    # For testing you can set any configs here
    mymodule.firstName = "Jaques";
  }];
}

Run and eval

ls -1 *.nix | \
entr sh -c 'nix-instantiate --eval ./sandbox.nix --strict -A config --json | jq'

Output

  "mymodule": {
    "firstName": "Jaques",
    "fullName": "Jaques Doe",
    "lastName": "Doe"
  }