diff options
author | Jan Tojnar <jtojnar@gmail.com> | 2020-12-17 18:19:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-17 18:19:11 +0100 |
commit | 5fb6fbcad99c7830fba8a283cd2026657b05dda4 (patch) | |
tree | 413cbd94eb933653fd6c34c158ad0ce0cb8ac8f6 /doc/builders | |
parent | fa0d499dbfa56be0b57b5d822702c624bb434dc1 (diff) | |
parent | 30bfb2f5d0a410bd61044f1b66b2c9f3d7589b9d (diff) |
Merge pull request #106845 from AndersonTorres/wip-documentation
Diffstat (limited to 'doc/builders')
-rw-r--r-- | doc/builders/fetchers.chapter.md | 70 | ||||
-rw-r--r-- | doc/builders/fetchers.xml | 150 | ||||
-rw-r--r-- | doc/builders/packages/index.xml | 2 | ||||
-rw-r--r-- | doc/builders/packages/locales.section.md | 5 | ||||
-rw-r--r-- | doc/builders/packages/locales.xml | 13 | ||||
-rw-r--r-- | doc/builders/special.xml | 4 | ||||
-rw-r--r-- | doc/builders/special/fhs-environments.section.md | 45 | ||||
-rw-r--r-- | doc/builders/special/fhs-environments.xml | 122 | ||||
-rw-r--r-- | doc/builders/special/mkshell.section.md | 15 | ||||
-rw-r--r-- | doc/builders/special/mkshell.xml | 24 | ||||
-rw-r--r-- | doc/builders/trivial-builders.chapter.md | 52 | ||||
-rw-r--r-- | doc/builders/trivial-builders.xml | 90 |
12 files changed, 190 insertions, 402 deletions
diff --git a/doc/builders/fetchers.chapter.md b/doc/builders/fetchers.chapter.md new file mode 100644 index 000000000000..d4cab056c70b --- /dev/null +++ b/doc/builders/fetchers.chapter.md @@ -0,0 +1,70 @@ +# Fetchers {#chap-pkgs-fetchers} + +When using Nix, you will frequently need to download source code and other files from the internet. Nixpkgs comes with a few helper functions that allow you to fetch fixed-output derivations in a structured way. + +The two fetcher primitives are `fetchurl` and `fetchzip`. Both of these have two required arguments, a URL and a hash. The hash is typically `sha256`, although many more hash algorithms are supported. Nixpkgs contributors are currently recommended to use `sha256`. This hash will be used by Nix to identify your source. A typical usage of fetchurl is provided below. + +```nix +{ stdenv, fetchurl }: + +stdenv.mkDerivation { + name = "hello"; + src = fetchurl { + url = "http://www.example.org/hello.tar.gz"; + sha256 = "1111111111111111111111111111111111111111111111111111"; + }; +} +``` + +The main difference between `fetchurl` and `fetchzip` is in how they store the contents. `fetchurl` will store the unaltered contents of the URL within the Nix store. `fetchzip` on the other hand will decompress the archive for you, making files and directories directly accessible in the future. `fetchzip` can only be used with archives. Despite the name, `fetchzip` is not limited to .zip files and can also be used with any tarball. + +`fetchpatch` works very similarly to `fetchurl` with the same arguments expected. It expects patch files as a source and and performs normalization on them before computing the checksum. For example it will remove comments or other unstable parts that are sometimes added by version control systems and can change over time. + + +Other fetcher functions allow you to add source code directly from a VCS such as subversion or git. These are mostly straightforward nambes based on the name of the command used with the VCS system. Because they give you a working repository, they act most like `fetchzip`. + +## `fetchsvn` + +Used with Subversion. Expects `url` to a Subversion directory, `rev`, and `sha256`. + +## `fetchgit` + +Used with Git. Expects `url` to a Git repo, `rev`, and `sha256`. `rev` in this case can be full the git commit id (SHA1 hash) or a tag name like `refs/tags/v1.0`. + +## `fetchfossil` + +Used with Fossil. Expects `url` to a Fossil archive, `rev`, and `sha256`. + +## `fetchcvs` + +Used with CVS. Expects `cvsRoot`, `tag`, and `sha256`. + +## `fetchhg` + +Used with Mercurial. Expects `url`, `rev`, and `sha256`. + +A number of fetcher functions wrap part of `fetchurl` and `fetchzip`. They are mainly convenience functions intended for commonly used destinations of source code in Nixpkgs. These wrapper fetchers are listed below. + +## `fetchFromGitHub` + +`fetchFromGitHub` expects four arguments. `owner` is a string corresponding to the GitHub user or organization that controls this repository. `repo` corresponds to the name of the software repository. These are located at the top of every GitHub HTML page as `owner`/`repo`. `rev` corresponds to the Git commit hash or tag (e.g `v1.0`) that will be downloaded from Git. Finally, `sha256` corresponds to the hash of the extracted directory. Again, other hash algorithms are also available but `sha256` is currently preferred. + +## `fetchFromGitLab` + +This is used with GitLab repositories. The arguments expected are very similar to fetchFromGitHub above. + +## `fetchFromGitiles` + +This is used with Gitiles repositories. The arguments expected are similar to fetchgit. + +## `fetchFromBitbucket` + +This is used with BitBucket repositories. The arguments expected are very similar to fetchFromGitHub above. + +## `fetchFromSavannah` + +This is used with Savannah repositories. The arguments expected are very similar to fetchFromGitHub above. + +## `fetchFromRepoOrCz` + +This is used with repo.or.cz repositories. The arguments expected are very similar to fetchFromGitHub above. diff --git a/doc/builders/fetchers.xml b/doc/builders/fetchers.xml deleted file mode 100644 index f07c310dcdf1..000000000000 --- a/doc/builders/fetchers.xml +++ /dev/null @@ -1,150 +0,0 @@ -<chapter xmlns="http://docbook.org/ns/docbook" - xmlns:xlink="http://www.w3.org/1999/xlink" - xmlns:xi="http://www.w3.org/2001/XInclude" - xml:id="chap-pkgs-fetchers"> - <title>Fetchers</title> - <para> - When using Nix, you will frequently need to download source code and other files from the internet. Nixpkgs comes with a few helper functions that allow you to fetch fixed-output derivations in a structured way. - </para> - <para> - The two fetcher primitives are <function>fetchurl</function> and <function>fetchzip</function>. Both of these have two required arguments, a URL and a hash. The hash is typically <literal>sha256</literal>, although many more hash algorithms are supported. Nixpkgs contributors are currently recommended to use <literal>sha256</literal>. This hash will be used by Nix to identify your source. A typical usage of fetchurl is provided below. - </para> -<programlisting><![CDATA[ -{ stdenv, fetchurl }: - -stdenv.mkDerivation { - name = "hello"; - src = fetchurl { - url = "http://www.example.org/hello.tar.gz"; - sha256 = "1111111111111111111111111111111111111111111111111111"; - }; -} -]]></programlisting> - <para> - The main difference between <function>fetchurl</function> and <function>fetchzip</function> is in how they store the contents. <function>fetchurl</function> will store the unaltered contents of the URL within the Nix store. <function>fetchzip</function> on the other hand will decompress the archive for you, making files and directories directly accessible in the future. <function>fetchzip</function> can only be used with archives. Despite the name, <function>fetchzip</function> is not limited to .zip files and can also be used with any tarball. - </para> - <para> - <function>fetchpatch</function> works very similarly to <function>fetchurl</function> with the same arguments expected. It expects patch files as a source and and performs normalization on them before computing the checksum. For example it will remove comments or other unstable parts that are sometimes added by version control systems and can change over time. - </para> - <para> - Other fetcher functions allow you to add source code directly from a VCS such as subversion or git. These are mostly straightforward names based on the name of the command used with the VCS system. Because they give you a working repository, they act most like <function>fetchzip</function>. - </para> - <variablelist> - <varlistentry> - <term> - <literal>fetchsvn</literal> - </term> - <listitem> - <para> - Used with Subversion. Expects <literal>url</literal> to a Subversion directory, <literal>rev</literal>, and <literal>sha256</literal>. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>fetchgit</literal> - </term> - <listitem> - <para> - Used with Git. Expects <literal>url</literal> to a Git repo, <literal>rev</literal>, and <literal>sha256</literal>. <literal>rev</literal> in this case can be full the git commit id (SHA1 hash) or a tag name like <literal>refs/tags/v1.0</literal>. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>fetchfossil</literal> - </term> - <listitem> - <para> - Used with Fossil. Expects <literal>url</literal> to a Fossil archive, <literal>rev</literal>, and <literal>sha256</literal>. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>fetchcvs</literal> - </term> - <listitem> - <para> - Used with CVS. Expects <literal>cvsRoot</literal>, <literal>tag</literal>, and <literal>sha256</literal>. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>fetchhg</literal> - </term> - <listitem> - <para> - Used with Mercurial. Expects <literal>url</literal>, <literal>rev</literal>, and <literal>sha256</literal>. - </para> - </listitem> - </varlistentry> - </variablelist> - <para> - A number of fetcher functions wrap part of <function>fetchurl</function> and <function>fetchzip</function>. They are mainly convenience functions intended for commonly used destinations of source code in Nixpkgs. These wrapper fetchers are listed below. - </para> - <variablelist> - <varlistentry> - <term> - <literal>fetchFromGitHub</literal> - </term> - <listitem> - <para> - <function>fetchFromGitHub</function> expects four arguments. <literal>owner</literal> is a string corresponding to the GitHub user or organization that controls this repository. <literal>repo</literal> corresponds to the name of the software repository. These are located at the top of every GitHub HTML page as <literal>owner</literal>/<literal>repo</literal>. <literal>rev</literal> corresponds to the Git commit hash or tag (e.g <literal>v1.0</literal>) that will be downloaded from Git. Finally, <literal>sha256</literal> corresponds to the hash of the extracted directory. Again, other hash algorithms are also available but <literal>sha256</literal> is currently preferred. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>fetchFromGitLab</literal> - </term> - <listitem> - <para> - This is used with GitLab repositories. The arguments expected are very similar to fetchFromGitHub above. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>fetchFromGitiles</literal> - </term> - <listitem> - <para> - This is used with Gitiles repositories. The arguments expected - are similar to fetchgit. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>fetchFromBitbucket</literal> - </term> - <listitem> - <para> - This is used with BitBucket repositories. The arguments expected are very similar to fetchFromGitHub above. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>fetchFromSavannah</literal> - </term> - <listitem> - <para> - This is used with Savannah repositories. The arguments expected are very similar to fetchFromGitHub above. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>fetchFromRepoOrCz</literal> - </term> - <listitem> - <para> - This is used with repo.or.cz repositories. The arguments expected are very similar to fetchFromGitHub above. - </para> - </listitem> - </varlistentry> - </variablelist> -</chapter> diff --git a/doc/builders/packages/index.xml b/doc/builders/packages/index.xml index c7a4aa9f47dc..e1ddfd276e20 100644 --- a/doc/builders/packages/index.xml +++ b/doc/builders/packages/index.xml @@ -14,7 +14,7 @@ <xi:include href="ibus.xml" /> <xi:include href="kakoune.section.xml" /> <xi:include href="linux.section.xml" /> - <xi:include href="locales.xml" /> + <xi:include href="locales.section.xml" /> <xi:include href="nginx.section.xml" /> <xi:include href="opengl.section.xml" /> <xi:include href="shell-helpers.section.xml" /> diff --git a/doc/builders/packages/locales.section.md b/doc/builders/packages/locales.section.md new file mode 100644 index 000000000000..e5a037004818 --- /dev/null +++ b/doc/builders/packages/locales.section.md @@ -0,0 +1,5 @@ +# Locales {#locales} + +To allow simultaneous use of packages linked against different versions of `glibc` with different locale archive formats Nixpkgs patches `glibc` to rely on `LOCALE_ARCHIVE` environment variable. + +On non-NixOS distributions this variable is obviously not set. This can cause regressions in language support or even crashes in some Nixpkgs-provided programs. The simplest way to mitigate this problem is exporting the `LOCALE_ARCHIVE` variable pointing to `${glibcLocales}/lib/locale/locale-archive`. The drawback (and the reason this is not the default) is the relatively large (a hundred MiB) size of the full set of locales. It is possible to build a custom set of locales by overriding parameters `allLocales` and `locales` of the package. diff --git a/doc/builders/packages/locales.xml b/doc/builders/packages/locales.xml deleted file mode 100644 index 44fdef034e77..000000000000 --- a/doc/builders/packages/locales.xml +++ /dev/null @@ -1,13 +0,0 @@ -<section xmlns="http://docbook.org/ns/docbook" - xmlns:xlink="http://www.w3.org/1999/xlink" - xml:id="locales"> - <title>Locales</title> - - <para> - To allow simultaneous use of packages linked against different versions of <literal>glibc</literal> with different locale archive formats Nixpkgs patches <literal>glibc</literal> to rely on <literal>LOCALE_ARCHIVE</literal> environment variable. - </para> - - <para> - On non-NixOS distributions this variable is obviously not set. This can cause regressions in language support or even crashes in some Nixpkgs-provided programs. The simplest way to mitigate this problem is exporting the <literal>LOCALE_ARCHIVE</literal> variable pointing to <literal>${glibcLocales}/lib/locale/locale-archive</literal>. The drawback (and the reason this is not the default) is the relatively large (a hundred MiB) size of the full set of locales. It is possible to build a custom set of locales by overriding parameters <literal>allLocales</literal> and <literal>locales</literal> of the package. - </para> -</section> diff --git a/doc/builders/special.xml b/doc/builders/special.xml index 15fdba9a0419..8902ce5c8132 100644 --- a/doc/builders/special.xml +++ b/doc/builders/special.xml @@ -5,6 +5,6 @@ <para> This chapter describes several special builders. </para> - <xi:include href="special/fhs-environments.xml" /> - <xi:include href="special/mkshell.xml" /> + <xi:include href="special/fhs-environments.section.xml" /> + <xi:include href="special/mkshell.section.xml" /> </chapter> diff --git a/doc/builders/special/fhs-environments.section.md b/doc/builders/special/fhs-environments.section.md new file mode 100644 index 000000000000..512a31cae0f1 --- /dev/null +++ b/doc/builders/special/fhs-environments.section.md @@ -0,0 +1,45 @@ +# buildFHSUserEnv {#sec-fhs-environments} + +`buildFHSUserEnv` provides a way to build and run FHS-compatible lightweight sandboxes. It creates an isolated root with bound `/nix/store`, so its 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. It uses Linux namespaces feature to create temporary lightweight environments which are destroyed after all child processes exit, without root user rights requirement. Accepted arguments are: + +- `name` + Environment name. +- `targetPkgs` + Packages to be installed for the main host's architecture (i.e. x86_64 on x86_64 installations). Along with libraries binaries are also installed. +- `multiPkgs` + Packages to be installed for all architectures supported by a host (i.e. i686 and x86_64 on x86_64 installations). Only libraries are installed by default. +- `extraBuildCommands` + Additional commands to be executed for finalizing the directory structure. +- `extraBuildCommandsMulti` + Like `extraBuildCommands`, but executed only on multilib architectures. +- `extraOutputsToInstall` + Additional derivation outputs to be linked for both target and multi-architecture packages. +- `extraInstallCommands` + Additional commands to be executed for finalizing the derivation with runner script. +- `runScript` + A command that would be executed inside the sandbox and passed all the command line arguments. It defaults to `bash`. + +One can create a simple environment using a `shell.nix` like that: + +```nix +{ pkgs ? import <nixpkgs> {} }: + +(pkgs.buildFHSUserEnv { + name = "simple-x11-env"; + targetPkgs = pkgs: (with pkgs; + [ udev + alsaLib + ]) ++ (with pkgs.xorg; + [ libX11 + libXcursor + libXrandr + ]); + multiPkgs = pkgs: (with pkgs; + [ udev + alsaLib + ]); + 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. diff --git a/doc/builders/special/fhs-environments.xml b/doc/builders/special/fhs-environments.xml deleted file mode 100644 index e7b81e97a23f..000000000000 --- a/doc/builders/special/fhs-environments.xml +++ /dev/null @@ -1,122 +0,0 @@ -<section xmlns="http://docbook.org/ns/docbook" - xmlns:xlink="http://www.w3.org/1999/xlink" - xmlns:xi="http://www.w3.org/2001/XInclude" - xml:id="sec-fhs-environments"> - <title>buildFHSUserEnv</title> - - <para> - <function>buildFHSUserEnv</function> provides a way to build and run FHS-compatible lightweight sandboxes. It creates an isolated root with bound <filename>/nix/store</filename>, so its 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. It uses Linux namespaces feature to create temporary lightweight environments which are destroyed after all child processes exit, without root user rights requirement. Accepted arguments are: - </para> - - <variablelist> - <varlistentry> - <term> - <literal>name</literal> - </term> - <listitem> - <para> - Environment name. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>targetPkgs</literal> - </term> - <listitem> - <para> - Packages to be installed for the main host's architecture (i.e. x86_64 on x86_64 installations). Along with libraries binaries are also installed. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>multiPkgs</literal> - </term> - <listitem> - <para> - Packages to be installed for all architectures supported by a host (i.e. i686 and x86_64 on x86_64 installations). Only libraries are installed by default. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>extraBuildCommands</literal> - </term> - <listitem> - <para> - Additional commands to be executed for finalizing the directory structure. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>extraBuildCommandsMulti</literal> - </term> - <listitem> - <para> - Like <literal>extraBuildCommands</literal>, but executed only on multilib architectures. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>extraOutputsToInstall</literal> - </term> - <listitem> - <para> - Additional derivation outputs to be linked for both target and multi-architecture packages. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>extraInstallCommands</literal> - </term> - <listitem> - <para> - Additional commands to be executed for finalizing the derivation with runner script. - </para> - </listitem> - </varlistentry> - <varlistentry> - <term> - <literal>runScript</literal> - </term> - <listitem> - <para> - A command that would be executed inside the sandbox and passed all the command line arguments. It defaults to <literal>bash</literal>. - </para> - </listitem> - </varlistentry> - </variablelist> - - <para> - One can create a simple environment using a <literal>shell.nix</literal> like that: - </para> - -<programlisting><![CDATA[ -{ pkgs ? import <nixpkgs> {} }: - -(pkgs.buildFHSUserEnv { - name = "simple-x11-env"; - targetPkgs = pkgs: (with pkgs; - [ udev - alsaLib - ]) ++ (with pkgs.xorg; - [ libX11 - libXcursor - libXrandr - ]); - multiPkgs = pkgs: (with pkgs; - [ udev - alsaLib - ]); - runScript = "bash"; -}).env -]]></programlisting> - - <para> - Running <literal>nix-shell</literal> 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 <literal>runScript</literal> to the application path, e.g. <filename>./bin/start.sh</filename> -- relative paths are supported. - </para> -</section> diff --git a/doc/builders/special/mkshell.section.md b/doc/builders/special/mkshell.section.md new file mode 100644 index 000000000000..1feb75cbd6f7 --- /dev/null +++ b/doc/builders/special/mkshell.section.md @@ -0,0 +1,15 @@ +# pkgs.mkShell {#sec-pkgs-mkShell} + +`pkgs.mkShell` is a special kind of derivation that is only useful when using it combined with `nix-shell`. It will in fact fail to instantiate when invoked with `nix-build`. + +## Usage {#sec-pkgs-mkShell-usage} + +```nix +{ pkgs ? import <nixpkgs> {} }: +pkgs.mkShell { + # this will make all the build inputs from hello and gnutar + # available to the shell environment + inputsFrom = with pkgs; [ hello gnutar ]; + buildInputs = [ pkgs.gnumake ]; +} +``` diff --git a/doc/builders/special/mkshell.xml b/doc/builders/special/mkshell.xml deleted file mode 100644 index cef65d06b882..000000000000 --- a/doc/builders/special/mkshell.xml +++ /dev/null @@ -1,24 +0,0 @@ -<section xmlns="http://docbook.org/ns/docbook" - xmlns:xlink="http://www.w3.org/1999/xlink" - xmlns:xi="http://www.w3.org/2001/XInclude" - xml:id="sec-pkgs-mkShell"> - <title>pkgs.mkShell</title> - - <para> - <function>pkgs.mkShell</function> is a special kind of derivation that is only useful when using it combined with <command>nix-shell</command>. It will in fact fail to instantiate when invoked with <command>nix-build</command>. - </para> - - <section xml:id="sec-pkgs-mkShell-usage"> - <title>Usage</title> - -<programlisting><![CDATA[ -{ pkgs ? import <nixpkgs> {} }: -pkgs.mkShell { - # this will make all the build inputs from hello and gnutar - # available to the shell environment - inputsFrom = with pkgs; [ hello gnutar ]; - buildInputs = [ pkgs.gnumake ]; -} -]]></programlisting> - </section> -</section> diff --git a/doc/builders/trivial-builders.chapter.md b/doc/builders/trivial-builders.chapter.md new file mode 100644 index 000000000000..c39803fbe339 --- /dev/null +++ b/doc/builders/trivial-builders.chapter.md @@ -0,0 +1,52 @@ +# Trivial builders {#chap-trivial-builders} + +Nixpkgs provides a couple of functions that help with building derivations. The most important one, `stdenv.mkDerivation`, has already been documented above. The following functions wrap `stdenv.mkDerivation`, making it easier to use in certain cases. + +## `runCommand` {#trivial-builder-runCommand} + +This takes three arguments, `name`, `env`, and `buildCommand`. `name` is just the name that Nix will append to the store path in the same way that `stdenv.mkDerivation` uses its `name` attribute. `env` is an attribute set specifying environment variables that will be set for this derivation. These attributes are then passed to the wrapped `stdenv.mkDerivation`. `buildCommand` specifies the commands that will be run to create this derivation. Note that you will need to create `$out` for Nix to register the command as successful. + +An example of using `runCommand` is provided below. + +```nix +(import <nixpkgs> {}).runCommand "my-example" {} '' + echo My example command is running + + mkdir $out + + echo I can write data to the Nix store > $out/message + + echo I can also run basic commands like: + + echo ls + ls + + echo whoami + whoami + + echo date + date +'' +``` + +## `runCommandCC` {#trivial-builder-runCommandCC} + +This works just like `runCommand`. The only difference is that it also provides a C compiler in `buildCommand`'s environment. To minimize your dependencies, you should only use this if you are sure you will need a C compiler as part of running your command. + +## `runCommandLocal` {#trivial-builder-runCommandLocal} + +Variant of `runCommand` that forces the derivation to be built locally, it is not substituted. This is intended for very cheap commands (<1s execution time). It saves on the network roundrip and can speed up a build. + +::: {.note} +This sets [`allowSubstitutes` to `false`](https://nixos.org/nix/manual/#adv-attr-allowSubstitutes), so only use `runCommandLocal` if you are certain the user will always have a builder for the `system` of the derivation. This should be true for most trivial use cases (e.g. just copying some files to a different location or adding symlinks), because there the `system` is usually the same as `builtins.currentSystem`. +::: + +## `writeTextFile`, `writeText`, `writeTextDir`, `writeScript`, `writeScriptBin` {#trivial-builder-writeText} + +These functions write `text` to the Nix store. This is useful for creating scripts from Nix expressions. `writeTextFile` takes an attribute set and expects two arguments, `name` and `text`. `name` corresponds to the name used in the Nix store path. `text` will be the contents of the file. You can also set `executable` to true to make this file have the executable bit set. + +Many more commands wrap `writeTextFile` including `writeText`, `writeTextDir`, `writeScript`, and `writeScriptBin`. These are convenience functions over `writeTextFile`. + +## `symlinkJoin` {#trivial-builder-symlinkJoin} + +This can be used to put many derivations into the same directory structure. It works by creating a new derivation and adding symlinks to each of the paths listed. It expects two arguments, `name`, and `paths`. `name` is the name used in the Nix store path for the created derivation. `paths` is a list of paths that will be symlinked. These paths can be to Nix store derivations or any other subdirectory contained within. diff --git a/doc/builders/trivial-builders.xml b/doc/builders/trivial-builders.xml deleted file mode 100644 index 94948c57b91f..000000000000 --- a/doc/builders/trivial-builders.xml +++ /dev/null @@ -1,90 +0,0 @@ -<chapter xmlns="http://docbook.org/ns/docbook" - xmlns:xlink="http://www.w3.org/1999/xlink" - xmlns:xi="http://www.w3.org/2001/XInclude" - xml:id="chap-trivial-builders"> - <title>Trivial builders</title> - <para> - Nixpkgs provides a couple of functions that help with building derivations. The most important one, <function>stdenv.mkDerivation</function>, has already been documented above. The following functions wrap <function>stdenv.mkDerivation</function>, making it easier to use in certain cases. - </para> - <variablelist> - <varlistentry xml:id="trivial-builder-runCommand"> - <term> - <literal>runCommand</literal> - </term> - <listitem> - <para> - This takes three arguments, <literal>name</literal>, <literal>env</literal>, and <literal>buildCommand</literal>. <literal>name</literal> is just the name that Nix will append to the store path in the same way that <literal>stdenv.mkDerivation</literal> uses its <literal>name</literal> attribute. <literal>env</literal> is an attribute set specifying environment variables that will be set for this derivation. These attributes are then passed to the wrapped <literal>stdenv.mkDerivation</literal>. <literal>buildCommand</literal> specifies the commands that will be run to create this derivation. Note that you will need to create <literal>$out</literal> for Nix to register the command as successful. - </para> - <para> - An example of using <literal>runCommand</literal> is provided below. - </para> -<programlisting> -(import <nixpkgs> {}).runCommand "my-example" {} '' - echo My example command is running - - mkdir $out - - echo I can write data to the Nix store > $out/message - - echo I can also run basic commands like: - - echo ls - ls - - echo whoami - whoami - - echo date - date -'' -</programlisting> - </listitem> - </varlistentry> - <varlistentry xml:id="trivial-builder-runCommandCC"> - <term> - <literal>runCommandCC</literal> - </term> - <listitem> - <para> - This works just like <literal>runCommand</literal>. The only difference is that it also provides a C compiler in <literal>buildCommand</literal>’s environment. To minimize your dependencies, you should only use this if you are sure you will need a C compiler as part of running your command. - </para> - </listitem> - </varlistentry> - <varlistentry xml:id="trivial-builder-runCommandLocal"> - <term> - <literal>runCommandLocal</literal> - </term> - <listitem> - <para> - Variant of <literal>runCommand</literal> that forces the derivation to be built locally, it is not substituted. This is intended for very cheap commands (<1s execution time). It saves on the network roundrip and can speed up a build. - </para> - <note><para> - This sets <link xlink:href="https://nixos.org/nix/manual/#adv-attr-allowSubstitutes"><literal>allowSubstitutes</literal> to <literal>false</literal></link>, so only use <literal>runCommandLocal</literal> if you are certain the user will always have a builder for the <literal>system</literal> of the derivation. This should be true for most trivial use cases (e.g. just copying some files to a different location or adding symlinks), because there the <literal>system</literal> is usually the same as <literal>builtins.currentSystem</literal>. - </para></note> - </listitem> - </varlistentry> - <varlistentry xml:id="trivial-builder-writeText"> - <term> - <literal>writeTextFile</literal>, <literal>writeText</literal>, <literal>writeTextDir</literal>, <literal>writeScript</literal>, <literal>writeScriptBin</literal> - </term> - <listitem> - <para> - These functions write <literal>text</literal> to the Nix store. This is useful for creating scripts from Nix expressions. <literal>writeTextFile</literal> takes an attribute set and expects two arguments, <literal>name</literal> and <literal>text</literal>. <literal>name</literal> corresponds to the name used in the Nix store path. <literal>text</literal> will be the contents of the file. You can also set <literal>executable</literal> to true to make this file have the executable bit set. - </para> - <para> - Many more commands wrap <literal>writeTextFile</literal> including <literal>writeText</literal>, <literal>writeTextDir</literal>, <literal>writeScript</literal>, and <literal>writeScriptBin</literal>. These are convenience functions over <literal>writeTextFile</literal>. - </para> - </listitem> - </varlistentry> - <varlistentry xml:id="trivial-builder-symlinkJoin"> - <term> - <literal>symlinkJoin</literal> - </term> - <listitem> - <para> - This can be used to put many derivations into the same directory structure. It works by creating a new derivation and adding symlinks to each of the paths listed. It expects two arguments, <literal>name</literal>, and <literal>paths</literal>. <literal>name</literal> is the name used in the Nix store path for the created derivation. <literal>paths</literal> is a list of paths that will be symlinked. These paths can be to Nix store derivations or any other subdirectory contained within. - </para> - </listitem> - </varlistentry> - </variablelist> -</chapter> |