···
246
+
<section xml:id="sec-declarative-package-management">
247
+
<title>Declarative Package Management</title>
249
+
<section xml:id="sec-building-environment">
250
+
<title>Build an environment</title>
253
+
Using <literal>packageOverrides</literal>, it is possible to manage
254
+
packages declaratively. This means that we can list all of our desired
255
+
packages within a declarative Nix expression. For example, to have
256
+
<literal>aspell</literal>, <literal>bc</literal>,
257
+
<literal>ffmpeg</literal>, <literal>coreutils</literal>,
258
+
<literal>gdb</literal>, <literal>nixUnstable</literal>,
259
+
<literal>emscripten</literal>, <literal>jq</literal>,
260
+
<literal>nox</literal>, and <literal>silver-searcher</literal>, we could
261
+
use the following in <filename>~/.config/nixpkgs/config.nix</filename>:
266
+
packageOverrides = pkgs: with pkgs; {
267
+
myPackages = pkgs.buildEnv {
268
+
name = "my-packages";
269
+
paths = [ aspell bc coreutils gdb ffmpeg nixUnstable emscripten jq nox silver-searcher ];
276
+
To install it into our environment, you can just run <literal>nix-env -iA
277
+
nixpkgs.myPackages</literal>. If you want to load the packages to be built
278
+
from a working copy of <literal>nixpkgs</literal> you just run
279
+
<literal>nix-env -f. -iA myPackages</literal>. To explore what's been
280
+
installed, just look through <filename>~/.nix-profile/</filename>. You can
281
+
see that a lot of stuff has been installed. Some of this stuff is useful
282
+
some of it isn't. Let's tell Nixpkgs to only link the stuff that we want:
287
+
packageOverrides = pkgs: with pkgs; {
288
+
myPackages = pkgs.buildEnv {
289
+
name = "my-packages";
290
+
paths = [ aspell bc coreutils gdb ffmpeg nixUnstable emscripten jq nox silver-searcher ];
291
+
pathsToLink = [ "/share" "/bin" ];
298
+
<literal>pathsToLink</literal> tells Nixpkgs to only link the paths listed
299
+
which gets rid of the extra stuff in the profile.
300
+
<filename>/bin</filename> and <filename>/share</filename> are good
301
+
defaults for a user environment, getting rid of the clutter. If you are
302
+
running on Nix on MacOS, you may want to add another path as well,
303
+
<filename>/Applications</filename>, that makes GUI apps available.
308
+
<section xml:id="sec-getting-documentation">
309
+
<title>Getting documentation</title>
312
+
After building that new environment, look through
313
+
<filename>~/.nix-profile</filename> to make sure everything is there that
314
+
we wanted. Discerning readers will note that some files are missing. Look
315
+
inside <filename>~/.nix-profile/share/man/man1/</filename> to verify this.
316
+
There are no man pages for any of the Nix tools! This is because some
317
+
packages like Nix have multiple outputs for things like documentation (see
318
+
section 4). Let's make Nix install those as well.
323
+
packageOverrides = pkgs: with pkgs; {
324
+
myPackages = pkgs.buildEnv {
325
+
name = "my-packages";
326
+
paths = [ aspell bc coreutils ffmpeg nixUnstable emscripten jq nox silver-searcher ];
327
+
pathsToLink = [ "/share/man" "/share/doc" /bin" ];
328
+
extraOutputsToInstall = [ "man" "doc" ];
335
+
This provides us with some useful documentation for using our packages.
336
+
However, if we actually want those manpages to be detected by man, we need
337
+
to set up our environment. This can also be managed within Nix
343
+
packageOverrides = pkgs: with pkgs; rec {
344
+
myProfile = writeText "my-profile" ''
345
+
export PATH=$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/sbin:/bin:/usr/sbin:/usr/bin
346
+
export MANPATH=$HOME/.nix-profile/share/man:/nix/var/nix/profiles/default/share/man:/usr/share/man
348
+
myPackages = pkgs.buildEnv {
349
+
name = "my-packages";
351
+
(runCommand "profile" {} ''
352
+
mkdir -p $out/etc/profile.d
353
+
cp ${myProfile} $out/etc/profile.d/my-profile.sh
366
+
pathsToLink = [ "/share/man" "/share/doc" /bin" "/etc" ];
367
+
extraOutputsToInstall = [ "man" "doc" ];
374
+
For this to work fully, you must also have this script sourced when you
375
+
are logged in. Try adding something like this to your
376
+
<filename>~/.profile</filename> file:
381
+
if [ -d $HOME/.nix-profile/etc/profile.d ]; then
382
+
for i in $HOME/.nix-profile/etc/profile.d/*.sh; do
391
+
Now just run <literal>source $HOME/.profile</literal> and you can starting
392
+
loading man pages from your environent.
397
+
<section xml:id="sec-gnu-info-setup">
398
+
<title>GNU info setup</title>
401
+
Configuring GNU info is a little bit trickier than man pages. To work
402
+
correctly, info needs a database to be generated. This can be done with
403
+
some small modifications to our environment scripts.
408
+
packageOverrides = pkgs: with pkgs; rec {
409
+
myProfile = writeText "my-profile" ''
410
+
export PATH=$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/sbin:/bin:/usr/sbin:/usr/bin
411
+
export MANPATH=$HOME/.nix-profile/share/man:/nix/var/nix/profiles/default/share/man:/usr/share/man
412
+
export INFOPATH=$HOME/.nix-profile/share/info:/nix/var/nix/profiles/default/share/info:/usr/share/info
414
+
myPackages = pkgs.buildEnv {
415
+
name = "my-packages";
417
+
(runCommand "profile" {} ''
418
+
mkdir -p $out/etc/profile.d
419
+
cp ${myProfile} $out/etc/profile.d/my-profile.sh
433
+
pathsToLink = [ "/share/man" "/share/doc" "/share/info" "/bin" "/etc" ];
434
+
extraOutputsToInstall = [ "man" "doc" "info" ];
436
+
if [ -x $out/bin/install-info -a -w $out/share/info ]; then
438
+
for i in $out/share/info/*.info $out/share/info/*.info.gz; do
439
+
$out/bin/install-info $i $out/share/info/dir
449
+
<literal>postBuild</literal> tells Nixpkgs to run a command after building
450
+
the environment. In this case, <literal>install-info</literal> adds the
451
+
installed info pages to <literal>dir</literal> which is GNU info's default
452
+
root node. Note that <literal>texinfoInteractive</literal> is added to the
453
+
environment to give the <literal>install-info</literal> command.