Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is ${modulesPath} is configuration.nix

Tags:

nix

nixos

In /etc/nixos/configuration.nix, I have this code

{ lib, pkgs, config, modulesPath, ... }:
with lib;
let
  nixos-wsl = import ./nixos-wsl;
in
{
  imports = [
    "${modulesPath}/profiles/minimal.nix"

    nixos-wsl.nixosModules.wsl
  ];

I would like to know what "${modulesPath} is.

I have tried in shell.

echo ${modulesPath}

nothing

I have tried to print it in a nix interpreter.

nix repl

${modulesPath}

error: syntax error, unexpected DOLLAR_CURLY modulePath

error: undefined variable 'modulesPath' nothing too.

Does somebody what is that and more generally how to get the value of "nix constant"

update

I missed something important:

I have to import it in nix repl like this.

nix repl

{modulesPath}: modulesPath

«lambda @ (string):1:1»

It say that it is a lamdba. I thought it would give a string value.

like image 436
Pierre-olivier Gendraud Avatar asked Oct 29 '25 22:10

Pierre-olivier Gendraud


1 Answers

Quoting from the nixpkgs source:

For NixOS, specialArgs includes modulesPath, which allows you to import extra modules from the nixpkgs package tree without having to somehow make the module aware of the location of the nixpkgs or NixOS directories.

{ modulesPath, ... }: {
   imports = [
     (modulesPath + "/profiles/minimal.nix")
   ];
}

This is performed in nixos/lib/eval-config-minimal.nix, as follows:

lib.evalModules {
  inherit prefix modules;
  specialArgs = {
    modulesPath = builtins.toString ../modules;
  } // specialArgs;
};

Because this is done in <nixpkgs>/nixos/lib, ../modules becomes <nixpkgs>/nixos/modules.

$ nix repl
Welcome to Nix 2.8.1. Type :? for help.

nix-repl> "${toString <nixpkgs>}/nixos/modules/profiles/minimal.nix"
"/nix/store/qdblsqzrzarf9am35r6nqnvlsl7dammk-source/nixos/modules/profiles/minimal.nix"

...run this on your own machine, and you'll get a directory that exists for you.

like image 122
Charles Duffy Avatar answered Nov 03 '25 00:11

Charles Duffy