How to import module from NixosConfiguration into repl


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 =;
    cfg = devCfg.python;
in { = with types; {
    enable = mkEnableOption "python";
    xdg.enable = mkOption {
      type = bool;
      default = devCfg.xdg.enable;

  config = mkMerge [
    (mkIf cfg.enable {
      user.packages = with pkgs; [
      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 = { inherit config, options, lib, pkgs;}
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?

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


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

with lib;
let cfg =;
in { = {
    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, you can do

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 = {inherit lib pkgs config options;};

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

:p test