Functions reference
The nixpkgs repository has several utility functions to manipulate Nix expressions.
pkgs.overridePackages
This function inside the nixpkgs expression (pkgs)
can be used to override the set of packages itself.
Warning: this function is expensive and must not be used from within
the nixpkgs repository.
Example usage:
let
pkgs = import <nixpkgs> {};
newpkgs = pkgs.overridePackages (self: super: {
foo = super.foo.override { ... };
};
in ...
The resulting newpkgs will have the new foo
expression, and all other expressions depending on foo will also
use the new foo expression.
The behavior of this function is similar to config.packageOverrides.
The self parameter refers to the final package set with the
applied overrides. Using this parameter may lead to infinite recursion if not
used consciously.
The super parameter refers to the old package set.
It's equivalent to pkgs in the above example.
<pkg>.override
The function override is usually available for all the
derivations in the nixpkgs expression (pkgs).
It is used to override the arguments passed to a function.
Example usages:
pkgs.foo.override { arg1 = val1; arg2 = val2; ... }pkgs.overridePackages (self: super: {
foo = super.foo.override { barSupport = true ; };
})mypkg = pkgs.callPackage ./mypkg.nix {
mydep = pkgs.mydep.override { ... };
})
In the first example, pkgs.foo is the result of a function call
with some default arguments, usually a derivation.
Using pkgs.foo.override will call the same function with
the given new arguments.
<pkg>.overrideDerivation
The function overrideDerivation is usually available for all the
derivations in the nixpkgs expression (pkgs).
It is used to create a new derivation by overriding the attributes of
the original derivation according to the given function.
Example usage:
mySed = pkgs.gnused.overrideDerivation (oldAttrs: {
name = "sed-4.2.2-pre";
src = fetchurl {
url = ftp://alpha.gnu.org/gnu/sed/sed-4.2.2-pre.tar.bz2;
sha256 = "11nq06d131y4wmf3drm0yk502d2xc6n5qy82cg88rb9nqd2lj41k";
};
patches = [];
});
In the above example, the name, src and patches of the derivation
will be overridden, while all other attributes will be retained from the
original derivation.
The argument oldAttrs is used to refer to the attribute set of
the original derivation.
lib.makeOverridable
The function lib.makeOverridable is used to make the result
of a function easily customizable. This utility only makes sense for functions
that accept an argument set and return an attribute set.
Example usage:
f = { a, b }: { result = a+b; }
c = lib.makeOverridable f { a = 1; b = 2; }
The variable c is the value of the f function
applied with some default arguments. Hence the value of c.result
is 3, in this example.
The variable c however also has some additional functions, like
c.override which can be used to
override the default arguments. In this example the value of
(c.override { a = 4; }).result is 6.
buildFHSChrootEnv/buildFHSUserEnvbuildFHSChrootEnv and
buildFHSUserEnv provide a way to build and run
FHS-compatible lightweight sandboxes. They get their own isolated root with
binded /nix/store, so their footprint in terms of disk
space needed is quite small. This allows one to run software which is hard or
unfeasible to patch for NixOS -- 3rd-party source trees with FHS assumptions,
games distributed as tarballs, software with integrity checking and/or external
self-updated binaries.
buildFHSChrootEnv allows to create persistent
environments, which can be constructed, deconstructed and entered by
multiple users at once. A downside is that it requires
root access for both those who create and destroy and
those who enter it. It can be useful to create environments for daemons that
one can enter and observe.
buildFHSUserEnv uses Linux namespaces feature to create
temporary lightweight environments which are destroyed after all child
processes exit. It does not require root access, and can be useful to create
sandboxes and wrap applications.
Those functions both rely on buildFHSEnv, which creates
an actual directory structure given a list of necessary packages and extra
build commands.
buildFHSChrootEnv and buildFHSUserEnv
both accept those arguments which are passed to
buildFHSEnv:
nameEnvironment name.targetPkgsPackages to be installed for the main host's architecture
(i.e. x86_64 on x86_64 installations).multiPkgsPackages to be installed for all architectures supported by
a host (i.e. i686 and x86_64 on x86_64 installations).extraBuildCommandsAdditional commands to be executed for finalizing the
directory structure.extraBuildCommandsMultiLike extraBuildCommandsMulti, but
executed only on multilib architectures.
Additionally, buildFHSUserEnv accepts
runScript parameter, which is a command that would be
executed inside the sandbox and passed all the command line arguments. It
default to bash.
It also uses CHROOTENV_EXTRA_BINDS environment variable
for binding extra directories in the sandbox to outside places. The format of
the variable is /mnt=test-mnt:/data, where
/mnt would be mounted as /test-mnt
and /data would be mounted as /data.
extraBindMounts array argument to
buildFHSUserEnv function is prepended to this variable.
Latter entries take priority if defined several times -- i.e. in case of
/data=data1:/data=data2 the actual bind path would be
/data2.
One can create a simple environment using a shell.nix
like that:
{} }:
(pkgs.buildFHSUserEnv {
name = "simple-x11-env";
targetPkgs = pkgs: (with pkgs;
[ udev
alsaLib
]) ++ (with pkgs.xorg;
[ libX11
libXcursor
libXrandr
]);
multiPkgs = pkgs: (with pkgs;
[ udev
alsaLib
]) ++ (with [];
runScript = "bash";
}).env
]]>
Running nix-shell would then drop you into a shell with
these libraries and binaries available. You can use this to run
closed-source applications which expect FHS structure without hassles:
simply change runScript to the application path,
e.g. ./bin/start.sh -- relative paths are supported.