1
0
Fork 0
mirror of https://github.com/NixOS/nix-pills synced 2024-09-19 04:00:13 -04:00
nix-pills/pills/02-install-on-your-running.xml

327 lines
11 KiB
XML
Raw Normal View History

2017-08-11 21:41:14 -04:00
<chapter xmlns="http://docbook.org/ns/docbook"
2017-08-11 18:22:51 -04:00
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
version="5.0"
2017-08-12 17:57:17 -04:00
xml:id="install-on-your-running-system">
2017-08-22 08:19:43 -04:00
<title>Install on Your Running System</title>
2017-08-12 17:57:17 -04:00
<para>
Welcome to the second Nix pill. In the <link
linkend="why-you-should-give-it-a-try">first</link> pill we
briefly described Nix.
</para>
<para>
Now we'll install Nix on our running system and understand what
changed in our system after the installation.
2018-12-23 16:44:05 -05:00
<emphasis role="bold">If you're using NixOS, Nix is already installed;
2018-12-11 15:02:41 -05:00
you can skip to the <link
2019-04-12 12:02:32 -04:00
linkend="enter-environment">next</link> pill.</emphasis>
2017-08-12 17:57:17 -04:00
</para>
<para>
2023-04-06 12:12:34 -04:00
For installation instructions, please refer to the Nix Reference Manual on
<link xlink:href="https://nixos.org/manual/nix/stable/installation/installation.html">
Installing Nix</link>.
2017-08-12 17:57:17 -04:00
</para>
<section>
<title>Installation</title>
<para>
2017-11-18 08:41:27 -05:00
These articles are not a tutorial on <emphasis>using</emphasis> Nix.
Instead, we're going to walk through the Nix system to understand the fundamentals.
2017-08-12 17:57:17 -04:00
</para>
<para>
2017-11-18 08:41:27 -05:00
The first thing to note: derivations in the Nix store refer to other
derivations which are themselves in the Nix store. They don't use <literal>libc</literal>
from our system or anywhere else. It's a self-contained store of all the software we need to bootstrap up
to any particular package.
2017-08-12 17:57:17 -04:00
</para>
2017-11-18 08:41:27 -05:00
<note><para>
In a multi-user installation, such as the one used in NixOS,
the store is owned by root and multiple users can install and build
2017-11-18 08:41:27 -05:00
software through a Nix daemon. You can read more about multi-user
installations here: <link
xlink:href="https://nixos.org/manual/nix/stable/installation/installing-binary.html#multi-user-installation">https://nixos.org/manual/nix/stable/installation/installing-binary.html#multi-user-installation</link>.
2017-11-18 08:41:27 -05:00
</para></note>
2017-08-12 17:57:17 -04:00
</section>
<section>
2017-11-18 08:41:27 -05:00
<title>The beginnings of the Nix store</title>
2017-08-12 17:57:17 -04:00
<para>
2017-11-18 08:41:27 -05:00
Start looking at the output of the install command:
2017-08-12 17:57:17 -04:00
</para>
<screen>copying Nix to /nix/store..........................</screen>
<para>
2017-11-18 08:41:27 -05:00
That's the <filename>/nix/store</filename> we
2020-10-07 10:21:41 -04:00
were talking about in the first article. We're copying in the
2017-08-12 17:57:17 -04:00
necessary software to bootstrap a Nix system. You can see bash,
2017-11-18 08:41:27 -05:00
coreutils, the C compiler toolchain, perl libraries, sqlite and Nix itself
2017-08-12 17:57:17 -04:00
with its own tools and libnix.
</para>
<para>
2017-11-18 08:41:27 -05:00
You may have noticed that <filename>/nix/store</filename> can contain
not only directories, but also files, still always in the form
2017-08-12 17:57:17 -04:00
<replaceable>hash-name</replaceable>.
</para>
</section>
<section>
2017-11-18 08:41:27 -05:00
<title>The Nix database</title>
2017-08-12 17:57:17 -04:00
<para>
Right after copying the store, the installation process
2017-11-18 08:41:27 -05:00
initializes a database:
2017-08-12 17:57:17 -04:00
</para>
<screen>initialising Nix database...</screen>
<para>
2017-11-18 08:41:27 -05:00
Yes, Nix also has a database. It's stored under
<filename>/nix/var/nix/db</filename>. It is a sqlite database
2017-08-12 17:57:17 -04:00
that keeps track of the dependencies between derivations.
</para>
<para>
The schema is very simple: there's a table of valid paths,
2017-11-18 08:41:27 -05:00
mapping from an auto increment integer to a store path.
2017-08-12 17:57:17 -04:00
</para>
<para>
2017-11-18 08:41:27 -05:00
Then there's a dependency relation from path to paths upon which they depend.
2017-08-12 17:57:17 -04:00
</para>
<para>
2017-11-18 08:41:27 -05:00
You can inspect the database by installing sqlite
2017-12-30 18:35:03 -05:00
(<command>nix-env -iA sqlite -f '&lt;nixpkgs&gt;'</command>) and then running
2017-11-18 08:41:27 -05:00
<command>sqlite3 /nix/var/nix/db/db.sqlite</command>.
2017-08-12 17:57:17 -04:00
</para>
<note><para>If this is the first time you're using Nix after the
initial installation, remember you must close and open your
2017-11-18 08:41:27 -05:00
terminals first, so that your shell environment will be updated.</para></note>
2017-11-18 08:41:27 -05:00
<important><para>
Never change <filename>/nix/store</filename> manually. If you do, then it will
no longer be in sync with the sqlite db, unless you <emphasis>really</emphasis>
know what you are doing.
</para></important>
2017-08-12 17:57:17 -04:00
</section>
<section>
<title>The first profile</title>
<para>
2017-11-18 08:41:27 -05:00
Next in the installation, we encounter the concept of the <link
xlink:href="https://nixos.org/manual/nix/stable/package-management/profiles.html">profile</link>:
2017-08-12 17:57:17 -04:00
</para>
<xi:include href="./02/user-environment.xml" parse="xml" />
<para>
2017-11-18 08:41:27 -05:00
A profile in Nix is a general and convenient concept for
2018-12-11 15:02:41 -05:00
realizing rollbacks. Profiles are used to compose
2017-11-18 08:41:27 -05:00
components that are spread among multiple paths under a new
unified path. Not only that, but profiles are made up of multiple
"generations": they are versioned. Whenever you change a profile,
2017-08-12 17:57:17 -04:00
a new generation is created.
</para>
<para>
2017-11-18 08:41:27 -05:00
Generations can be switched and rolled back atomically, which makes
them convenient for managing changes to your system.
2017-08-12 17:57:17 -04:00
</para>
<para>
Let's take a closer look at our profile:
</para>
<xi:include href="./02/profile.xml" parse="xml" />
<para>
2018-12-05 18:56:44 -05:00
That <package>nix-2.1.3</package> derivation in the Nix store is
2018-12-11 15:02:41 -05:00
Nix itself, with binaries and libraries. The process of "installing"
2017-11-18 08:41:27 -05:00
the derivation in the profile basically reproduces the hierarchy of the
2018-12-05 18:56:44 -05:00
<package>nix-2.1.3</package> store derivation in the profile by means of
2017-08-12 17:57:17 -04:00
symbolic links.
</para>
<para>
The contents of this profile are special, because only one
program has been installed in our profile, therefore e.g. the
2017-11-18 08:41:27 -05:00
<filename>bin</filename> directory points to the only program
which has been installed (Nix itself).
2017-08-12 17:57:17 -04:00
</para>
<para>
But that's only the contents of the latest generation of our
2017-11-18 08:41:27 -05:00
profile. In fact, <filename>~/.nix-profile</filename> itself is a
2017-08-12 17:57:17 -04:00
symbolic link to
2017-11-18 08:41:27 -05:00
<filename>/nix/var/nix/profiles/default</filename>.
2017-08-12 17:57:17 -04:00
</para>
<para>
2017-11-18 08:41:27 -05:00
In turn, that's a symlink to <filename>default-1-link</filename>
in the same directory. Yes, that means it's the first generation of
2017-08-12 17:57:17 -04:00
the <literal>default</literal> profile.
</para>
<para>
2017-11-18 08:41:27 -05:00
Finally, <filename>default-1-link</filename> is a symlink to the nix
store "user-environment" derivation that you saw printed during the installation process.
2017-08-12 17:57:17 -04:00
</para>
<para>
2017-11-18 08:41:27 -05:00
We'll talk about <filename>manifest.nix</filename> more in the next article.
2017-08-12 17:57:17 -04:00
</para>
</section>
<section>
2017-11-18 08:41:27 -05:00
<title>Nixpkgs expressions</title>
2017-08-12 17:57:17 -04:00
<para>
2017-11-18 08:41:27 -05:00
More output from the installer:
2017-08-12 17:57:17 -04:00
</para>
<screen><xi:include href="./02/nixpkgs-expressions.txt"
parse="text" /></screen>
<para>
<link
xlink:href="https://nixos.org/manual/nix/stable/expressions/writing-nix-expressions.html">Nix
2017-08-12 17:57:17 -04:00
expressions</link> are used to describe packages and how to
build them. <link
xlink:href="https://nixos.org/nixpkgs/">Nixpkgs</link> is the
2017-11-18 08:41:27 -05:00
repository containing all of the expressions: <link
2017-08-12 17:57:17 -04:00
xlink:href="https://github.com/NixOS/nixpkgs">https://github.com/NixOS/nixpkgs</link>.
</para>
<para>
The installer downloaded the package descriptions from commit
<literal>a1a2851</literal>.
</para>
<para>
2017-11-18 08:41:27 -05:00
The second profile we discover is the channels profile.
2017-08-12 17:57:17 -04:00
<filename>~/.nix-defexpr/channels</filename> points to
<filename>/nix/var/nix/profiles/per-user/nix/channels</filename>
which points to <literal>channels-1-link</literal> which points
2017-11-18 08:41:27 -05:00
to a Nix store directory containing the downloaded Nix
2017-08-12 17:57:17 -04:00
expressions.
</para>
<para>
Channels are a set of packages and expressions available for
2017-11-18 08:41:27 -05:00
download. Similar to Debian stable and unstable, there's a
2017-08-12 17:57:17 -04:00
stable and unstable channel. In this installation, we're
2017-11-18 08:41:27 -05:00
tracking <literal>nixpkgs-unstable</literal>.
2017-08-12 17:57:17 -04:00
</para>
<para>
2017-11-18 08:41:27 -05:00
Don't worry about Nix expressions yet, we'll get to them later.
2017-08-12 17:57:17 -04:00
</para>
<para>
2017-11-18 08:41:27 -05:00
Finally, for your convenience, the installer modified
<filename>~/.profile</filename> to automatically enter the Nix
2017-08-12 17:57:17 -04:00
environment. What
<filename>~/.nix-profile/etc/profile.d/nix.sh</filename> really
2017-11-18 08:41:27 -05:00
does is simply to add <filename>~/.nix-profile/bin</filename> to
2017-08-12 17:57:17 -04:00
<varname>PATH</varname> and
<filename>~/.nix-defexpr/channels/nixpkgs</filename> to
2018-12-11 15:02:41 -05:00
<varname>NIX_PATH</varname>. We'll discuss
2017-11-18 08:41:27 -05:00
<varname>NIX_PATH</varname> later.
2017-08-12 17:57:17 -04:00
</para>
<para>
Read <filename>nix.sh</filename>, it's short.
</para>
</section>
<section>
<title>FAQ: Can I change /nix to something else?</title>
<para>
2017-11-18 08:41:27 -05:00
You can, but there's a good reason to keep using
<filename>/nix</filename> instead of a different directory. All
the derivations depend on other derivations by using absolute paths. We
saw in the first article that bash referenced a
<package>glibc</package> under a specific absolute path in <filename>/nix/store</filename>.
2017-08-12 17:57:17 -04:00
</para>
<para>
2017-11-18 08:41:27 -05:00
You can see for yourself, don't worry if you see multiple
2017-08-12 17:57:17 -04:00
bash derivations:
</para>
<screen><xi:include href="./02/ldd-bash.txt" parse="text" /></screen>
<para>
Keeping the store in <filename>/nix</filename> means we can grab
the binary cache from nixos.org (just like you grab packages
from debian mirrors) otherwise:
<itemizedlist>
2017-11-18 08:41:27 -05:00
<listitem><para>
<package>glibc</package> would be installed under <filename>/foo/store</filename>
</para></listitem>
<listitem><para>
Thus bash would need to point to <package>glibc</package> under <filename>/foo/store</filename>,
instead of under <filename>/nix/store</filename>
</para></listitem>
<listitem><para>
So the binary cache can't help, because we need a <emphasis>different</emphasis> bash,
and so we'd have to recompile everything ourselves.
</para></listitem>
2017-08-12 17:57:17 -04:00
</itemizedlist>
</para>
<para>
2017-11-18 08:41:27 -05:00
After all <filename>/nix</filename> is a sensible place for the store.
2017-08-12 17:57:17 -04:00
</para>
</section>
<section>
<title>Conclusion</title>
<para>
2017-11-18 08:41:27 -05:00
We've installed Nix on our system, fully isolated and owned by
the <literal>nix</literal> user as we're still coming to terms with
2017-08-12 17:57:17 -04:00
this new system.
</para>
<para>
We learned some new concepts like profiles and channels. In
particular, with profiles we're able to manage multiple
generations of a composition of packages, while with channels
2017-11-18 08:41:27 -05:00
we're able to download binaries from <literal>nixos.org</literal>.
2017-08-12 17:57:17 -04:00
</para>
<para>
The installation put everything under <filename>/nix</filename>,
2017-11-18 08:41:27 -05:00
and some symlinks in the Nix user home. That's because every
2017-08-12 17:57:17 -04:00
user is able to install and use software in her own environment.
</para>
<para>
2017-11-18 08:41:27 -05:00
I hope I left nothing uncovered so that you think there's
some kind of magic going on behind the scenes. It's all
about putting components in the store and symlinking
these components together.
2017-08-12 17:57:17 -04:00
</para>
</section>
<section>
<title>Next pill...</title>
<para>
2017-11-18 08:41:27 -05:00
...we will enter the Nix environment and learn how to interact
2017-08-12 17:57:17 -04:00
with the store.
</para>
</section>
2017-08-11 18:22:51 -04:00
2017-08-11 21:41:14 -04:00
</chapter>