NixOS modules: Difference between revisions

imported>Makefu
add example module
imported>Tilpner
Describe how to use imports without committing to a directory structure
Line 126: Line 126:
           then { port = 8000; }
           then { port = 8000; }
           else { listen = [ { addr = "0.0.0.0"; port = 8000; } ]; });
           else { listen = [ { addr = "0.0.0.0"; port = 8000; } ]; });
}
</syntaxhighlight>
== Abstract imports ==
To import a module that's stored ''somewhere'' (but for which you have neither an absolute nor a relative path), you can use [https://github.com/musnix/musnix#basic-usage NIX_PATH elements] or <code>specialArgs</code> from <code>nixos/lib/eval-config.nix</code>.
This is useful for e.g. pulling modules from a git repository without adding it as a channel, or if you just prefer using paths relative to a root you can change (as opposed to the current file, which could move in the future).
<syntaxhighlight lang="nix">
let
  inherit (import <nixpkgs> {}) writeShellScriptBin fetchgit;
  yourModules = fetchgit { ... };
in rec {
  nixos = import <nixpkgs/nixos/lib/eval-config.nix> {
    modules = [ ./configuration.nix ];
    specialArgs.mod = name: "${yourModules}/${name}";
  };
  /* use nixos here, e.g. for deployment or building an image */
}
</syntaxhighlight>
<syntaxhighlight lang="nix">
{ config, lib, pkgs, mod, ... }: {
  imports = [
    (mod "foo.nix")
  ];
  ...
}
}
</syntaxhighlight>
</syntaxhighlight>