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
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
{ 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'
This thread comes up top in Google when searching for how to import lib and in order to evaluate and print a module. For those landing here in a similar situation and looking for an easy solution:
From the directory containing the file, run :p import ./yourModule.nix {lib = import <nixpkgs/lib>;}.
(replacing yourModule.nix with the file you wish to evaluate. It needs to be passed as a path, not a string, hence ./)
That’s probably not going to work; most modules depend on other modules.
As @ElvishJerricco mentioned, you really want to use lib.nixosSystem (which is a thin wrapper around lib.evalModules) to do any evaluations.