With-Expression
Deconstruct from set into scope
with
keyword can be used to introduce variables from a attribute set into current scope:
nix
let foo = { bar = "???"; baz = "!!!" }; in
${foo.bar} + ${foo.baz} # `bar` and `baz` are introduced
1
2
3
2
3
equivalent to
nix
let foo = { bar = "???"; baz = "!!!" }; in
with foo;
${bar} + ${baz} # `bar` and `baz` are introduced
1
2
3
2
3
nix
f = { x ? 1, y ? 2, ... }@args: with args; x + y + z
1
nix
foo = rec {
p = { x = 1; y = 1; };
bar = with p; [ x y ];
};
1
2
3
4
2
3
4
Importing from another module
When another module is imported, all attributes will be deconstructed from the set into scope.
nix
let foo = null; in
with (import ./foo.nix);
1
2
2
Practical usage
nix
{ config, pkgs, ... }:
{
home.packages = with pkgs; [
git
ripgrep
neovim
];
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9