git.iko.soy/typeable/nixpkgs
on master help sign in
Overlays: allow overlays to be specified in a file
commit9235ff1259bb9425c58ac3b3322ddf83e74a4255 authorMichael Peyton Jones <michaelpj@gmail.com> dateWed 9 Aug 2017, 19:47 (9 years ago) parent060a41d
2 files changed+70−26
doc/overlays.xml+41−15 pkgs/top-level/impure.nix+29−11
doc/overlays.xml+41−15
@@ -17,42 +17,68 @@ if multiple layers override the same package.</para>
1717<section xml:id="sec-overlays-install">
1818<title>Installing overlays</title>
1919
20−<para>The list of overlays is determined as follows:
20+<para>The list of overlays is determined as follows.</para>
21+
22+<para>If the <varname>overlays</varname> argument is not provided explicitly, we look for overlays in a path. The path
23+is determined as follows:
2124
2225<orderedlist>
2326
2427 <listitem>
2528 <para>First, if an <varname>overlays</varname> argument to the nixpkgs function itself is given,
26− then that is used. This can be passed explicitly when importing nipxkgs, for example
27− <literal>import &lt;nixpkgs> { overlays = [ overlay1 overlay2 ] }</literal>.</para>
29+ then that is used.</para>
2830
29− <para>On a NixOS system the value of the <literal>nixpkgs.overlays</literal> option, if present,
30− is passed to the system Nixpkgs in this way. Note that this does not affect the overlays for
31− non-NixOS operations (e.g. <literal>nix-env</literal>), which are looked up independently.</para>
31+ <para>This can be passed explicitly when importing nipxkgs, for example
32+ <literal>import &lt;nixpkgs> { overlays = [ overlay1 overlay2 ]; }</literal>.</para>
3233 </listitem>
3334
3435 <listitem>
35− <para>Otherwise, if the Nix path entry <literal>&lt;nixpkgs-overlays></literal> exists and is a
36− directory, then the result is the set of overlays found in that directory, ordered lexicographically.</para>
36+ <para>Otherwise, if the Nix path entry <literal>&lt;nixpkgs-overlays></literal> exists, we look for overlays
37+ at that path, as described below.</para>
3738
3839 <para>See the section on <literal>NIX_PATH</literal> in the Nix manual for more details on how to
3940 set a value for <literal>&lt;nixpkgs-overlays>.</literal></para>
4041 </listitem>
4142
4243 <listitem>
43− <para>Otherwise, if <filename>~/.config/nixpkgs/overlays/</filename> exists and is a directory, then
44− the result is the set of overlays found in that directory, ordered lexicographically.</para>
44+ <para>If one of <filename>~/.config/nixpkgs/overlays.nix</filename> and
45+ <filename>~/.config/nixpkgs/overlays/</filename> exists, then we look for overlays at that path, as
46+ described below. It is an error if both exist.</para>
4547 </listitem>
4648
4749</orderedlist>
4850</para>
4951
50−<para>For the second and third options, overlays are extracted from the given directory as files,
51−directories containing a <filename>default.nix</filename>, or symlinks to one of those.</para>
52+<para>If we are looking for overlays at a path, then there are two cases:
53+<itemizedlist>
54+ <listitem>
55+ <para>If the path is a file, then the file is imported as a Nix expression and used as the list of
56+ overlays.</para>
57+ </listitem>
58+
59+ <listitem>
60+ <para>If the path is a directory, then we take the content of the directory, order it
61+ lexicographically, and attempt to interpret each as an overlay by:
62+ <itemizedlist>
63+ <listitem>
64+ <para>Importing the file, if it is a <literal>.nix</literal> file.</para>
65+ </listitem>
66+ <listitem>
67+ <para>Importing a top-level <filename>default.nix</filename> file, if it is a directory.</para>
68+ </listitem>
69+ </itemizedlist>
70+ </para>
71+ </listitem>
72+</itemizedlist>
73+</para>
74+
75+<para>On a NixOS system the value of the <literal>nixpkgs.overlays</literal> option, if present,
76+is passed to the system Nixpkgs directly as an argument. Note that this does not affect the overlays for
77+non-NixOS operations (e.g. <literal>nix-env</literal>), which are looked up independently.</para>
5278
53−<para>The last option provides a convenient way to install an overlay from a repository,
54−by cloning the overlay's repository and adding a symbolic link to it in
55−<filename>~/.config/nixpkgs/overlays/</filename>.</para>
79+<para>The <filename>overlays.nix</filename> option therefore provides a convenient way to use the same
80+overlays for a NixOS system configuration and user configuration: the same file can be used
81+as <filename>overlays.nix</filename> and imported as the value of <literal>nixpkgs.overlays</literal>.</para>
5682
5783</section>
5884
pkgs/top-level/impure.nix+29−11
@@ -40,18 +40,36 @@ in
4040 # collections of packages. These collection of packages are part of the
4141 # fix-point made by Nixpkgs.
4242 overlays ? let
43− dirPath = try (if pathExists <nixpkgs-overlays> then <nixpkgs-overlays> else "") "";
44− dirHome = homeDir + "/.config/nixpkgs/overlays";
45− dirCheck = dir: dir != "" && pathExists (dir + "/.");
46− overlays = dir:
47− let content = readDir dir; in
48− map (n: import (dir + ("/" + n)))
49− (builtins.filter (n: builtins.match ".*\.nix" n != null || pathExists (dir + ("/" + n + "/default.nix")))
50− (attrNames content));
43+ isDir = path: pathExists (path + "/.");
44+ pathOverlays = try <nixpkgs-overlays> "";
45+ homeOverlaysFile = homeDir + "/.config/nixpkgs/overlays.nix";
46+ homeOverlaysDir = homeDir + "/.config/nixpkgs/overlays";
47+ overlays = path:
48+ # check if the path is a directory or a file
49+ if isDir path then
50+ # it's a directory, so the set of overlays from the directory, ordered lexicographically
51+ let content = readDir path; in
52+ map (n: import (path + ("/" + n)))
53+ (builtins.filter (n: builtins.match ".*\.nix" n != null || pathExists (path + ("/" + n + "/default.nix")))
54+ (attrNames content))
55+ else
56+ # it's a file, so the result is the contents of the file itself
57+ import path;
5158 in
52− if dirPath != "" then
53− overlays dirPath
54− else if dirCheck dirHome then overlays dirHome
59+ if pathOverlays != "" && pathExists pathOverlays then overlays pathOverlays
60+ else if pathExists homeOverlaysFile && pathExists homeOverlaysDir then
61+ throw ''
62+ Nixpkgs overlays can be specified with ${homeOverlaysFile} or ${homeOverlaysDir}, but not both.
63+ Please remove one of them and try again.
64+ ''
65+ else if pathExists homeOverlaysFile then
66+ if isDir homeOverlaysFile then
67+ throw (homeOverlaysFile + " should be a file")
68+ else overlays homeOverlaysFile
69+ else if pathExists homeOverlaysDir then
70+ if !(isDir homeOverlaysDir) then
71+ throw (homeOverlaysDir + " should be a directory")
72+ else overlays homeOverlaysDir
5573 else []
5674
5775, ...