doc: update Nix code snippets format
Command: `mdcr --config doc/tests/mdcr-config.toml doc/`
This commit is contained in:
committed by
Valentin Gagarin
parent
5d979e79ce
commit
bcea0cf344
@@ -42,9 +42,15 @@ This function does not support `__structuredAttrs`, but does support `passAsFile
|
||||
devShellTools.unstructuredDerivationInputEnv {
|
||||
drvAttrs = {
|
||||
name = "foo";
|
||||
buildInputs = [ hello figlet ];
|
||||
buildInputs = [
|
||||
hello
|
||||
figlet
|
||||
];
|
||||
builder = bash;
|
||||
args = [ "-c" "${./builder.sh}" ];
|
||||
args = [
|
||||
"-c"
|
||||
"${./builder.sh}"
|
||||
];
|
||||
};
|
||||
}
|
||||
# => {
|
||||
@@ -69,7 +75,10 @@ Takes the relevant parts of a derivation and returns a set of environment variab
|
||||
let
|
||||
pkg = hello;
|
||||
in
|
||||
devShellTools.derivationOutputEnv { outputList = pkg.outputs; outputMap = pkg; }
|
||||
devShellTools.derivationOutputEnv {
|
||||
outputList = pkg.outputs;
|
||||
outputMap = pkg;
|
||||
}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
@@ -491,7 +491,11 @@ It might be useful to manipulate the content downloaded by `fetchurl` directly i
|
||||
In this example, we'll adapt [](#ex-fetchers-fetchurl-nixpkgs-version) to append the result of running the `hello` package to the contents we download, purely to illustrate how to manipulate the content.
|
||||
|
||||
```nix
|
||||
{ fetchurl, hello, lib }:
|
||||
{
|
||||
fetchurl,
|
||||
hello,
|
||||
lib,
|
||||
}:
|
||||
fetchurl {
|
||||
url = "https://raw.githubusercontent.com/NixOS/nixpkgs/23.11/.version";
|
||||
|
||||
@@ -714,9 +718,10 @@ A wrapper around `fetchpatch`, which takes:
|
||||
Here is an example of `fetchDebianPatch` in action:
|
||||
|
||||
```nix
|
||||
{ lib
|
||||
, fetchDebianPatch
|
||||
, buildPythonPackage
|
||||
{
|
||||
lib,
|
||||
fetchDebianPatch,
|
||||
buildPythonPackage,
|
||||
}:
|
||||
|
||||
buildPythonPackage rec {
|
||||
@@ -914,7 +919,9 @@ It produces packages that cannot be built automatically.
|
||||
{ fetchtorrent }:
|
||||
|
||||
fetchtorrent {
|
||||
config = { peer-limit-global = 100; };
|
||||
config = {
|
||||
peer-limit-global = 100;
|
||||
};
|
||||
url = "magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c";
|
||||
hash = "";
|
||||
}
|
||||
|
||||
@@ -66,7 +66,8 @@ let
|
||||
url = "https://github.com/irccloud/irccloud-desktop/releases/download/v${version}/IRCCloud-${version}-linux-x86_64.AppImage";
|
||||
hash = "sha256-/hMPvYdnVB1XjKgU2v47HnVvW4+uC3rhRjbucqin4iI=";
|
||||
};
|
||||
in appimageTools.wrapType2 {
|
||||
in
|
||||
appimageTools.wrapType2 {
|
||||
inherit pname version src;
|
||||
extraPkgs = pkgs: [ pkgs.at-spi2-core ];
|
||||
}
|
||||
@@ -106,7 +107,8 @@ let
|
||||
appimageContents = appimageTools.extract {
|
||||
inherit pname version src;
|
||||
};
|
||||
in appimageTools.wrapType2 {
|
||||
in
|
||||
appimageTools.wrapType2 {
|
||||
inherit pname version src;
|
||||
|
||||
extraPkgs = pkgs: [ pkgs.at-spi2-core ];
|
||||
@@ -150,7 +152,8 @@ let
|
||||
substituteInPlace $out/irccloud.desktop --replace-fail 'Exec=AppRun' 'Exec=${pname}'
|
||||
'';
|
||||
};
|
||||
in appimageTools.wrapType2 {
|
||||
in
|
||||
appimageTools.wrapType2 {
|
||||
inherit pname version src;
|
||||
|
||||
extraPkgs = pkgs: [ pkgs.at-spi2-core ];
|
||||
|
||||
@@ -35,7 +35,7 @@ The following derivation will construct a flat-file binary cache containing the
|
||||
```nix
|
||||
{ mkBinaryCache, hello }:
|
||||
mkBinaryCache {
|
||||
rootPaths = [hello];
|
||||
rootPaths = [ hello ];
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -235,7 +235,11 @@ The following package builds a Docker image that runs the `redis-server` executa
|
||||
The Docker image will have name `redis` and tag `latest`.
|
||||
|
||||
```nix
|
||||
{ dockerTools, buildEnv, redis }:
|
||||
{
|
||||
dockerTools,
|
||||
buildEnv,
|
||||
redis,
|
||||
}:
|
||||
dockerTools.buildImage {
|
||||
name = "redis";
|
||||
tag = "latest";
|
||||
@@ -253,7 +257,9 @@ dockerTools.buildImage {
|
||||
config = {
|
||||
Cmd = [ "/bin/redis-server" ];
|
||||
WorkingDir = "/data";
|
||||
Volumes = { "/data" = { }; };
|
||||
Volumes = {
|
||||
"/data" = { };
|
||||
};
|
||||
};
|
||||
}
|
||||
```
|
||||
@@ -286,7 +292,11 @@ It uses `runAsRoot` to create a directory and a file inside the image.
|
||||
This works the same as [](#ex-dockerTools-buildImage-extraCommands), but uses `runAsRoot` instead of `extraCommands`.
|
||||
|
||||
```nix
|
||||
{ dockerTools, buildEnv, hello }:
|
||||
{
|
||||
dockerTools,
|
||||
buildEnv,
|
||||
hello,
|
||||
}:
|
||||
dockerTools.buildImage {
|
||||
name = "hello";
|
||||
tag = "latest";
|
||||
@@ -320,7 +330,11 @@ This works the same as [](#ex-dockerTools-buildImage-runAsRoot), but uses `extra
|
||||
Note that with `extraCommands`, we can't directly reference `/` and must create files and directories as if we were already on `/`.
|
||||
|
||||
```nix
|
||||
{ dockerTools, buildEnv, hello }:
|
||||
{
|
||||
dockerTools,
|
||||
buildEnv,
|
||||
hello,
|
||||
}:
|
||||
dockerTools.buildImage {
|
||||
name = "hello";
|
||||
tag = "latest";
|
||||
@@ -350,7 +364,11 @@ dockerTools.buildImage {
|
||||
Note that using a value of `"now"` in the `created` attribute will break reproducibility.
|
||||
|
||||
```nix
|
||||
{ dockerTools, buildEnv, hello }:
|
||||
{
|
||||
dockerTools,
|
||||
buildEnv,
|
||||
hello,
|
||||
}:
|
||||
dockerTools.buildImage {
|
||||
name = "hello";
|
||||
tag = "latest";
|
||||
@@ -766,7 +784,11 @@ The closure of `config` is automatically included in the generated image.
|
||||
The following package shows a more compact way to create the same output generated in [](#ex-dockerTools-streamLayeredImage-hello).
|
||||
|
||||
```nix
|
||||
{ dockerTools, hello, lib }:
|
||||
{
|
||||
dockerTools,
|
||||
hello,
|
||||
lib,
|
||||
}:
|
||||
dockerTools.streamLayeredImage {
|
||||
name = "hello";
|
||||
tag = "latest";
|
||||
@@ -1547,11 +1569,15 @@ The Docker image generated will have a name like `hello-<version>-env` and tag `
|
||||
This example uses [](#ex-dockerTools-streamNixShellImage-hello) as a starting point.
|
||||
|
||||
```nix
|
||||
{ dockerTools, cowsay, hello }:
|
||||
{
|
||||
dockerTools,
|
||||
cowsay,
|
||||
hello,
|
||||
}:
|
||||
dockerTools.streamNixShellImage {
|
||||
tag = "latest";
|
||||
drv = hello.overrideAttrs (old: {
|
||||
nativeBuildInputs = old.nativeBuildInputs or [] ++ [
|
||||
nativeBuildInputs = old.nativeBuildInputs or [ ] ++ [
|
||||
cowsay
|
||||
];
|
||||
});
|
||||
|
||||
@@ -52,23 +52,23 @@ A `deterministic` flag is available for best efforts determinism.
|
||||
To produce a Nix-store only image:
|
||||
```nix
|
||||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
pkgs = import <nixpkgs> { };
|
||||
lib = pkgs.lib;
|
||||
make-disk-image = import <nixpkgs/nixos/lib/make-disk-image.nix>;
|
||||
in
|
||||
make-disk-image {
|
||||
inherit pkgs lib;
|
||||
config = {};
|
||||
additionalPaths = [ ];
|
||||
format = "qcow2";
|
||||
onlyNixStore = true;
|
||||
partitionTableType = "none";
|
||||
installBootLoader = false;
|
||||
touchEFIVars = false;
|
||||
diskSize = "auto";
|
||||
additionalSpace = "0M"; # Defaults to 512M.
|
||||
copyChannel = false;
|
||||
}
|
||||
make-disk-image {
|
||||
inherit pkgs lib;
|
||||
config = { };
|
||||
additionalPaths = [ ];
|
||||
format = "qcow2";
|
||||
onlyNixStore = true;
|
||||
partitionTableType = "none";
|
||||
installBootLoader = false;
|
||||
touchEFIVars = false;
|
||||
diskSize = "auto";
|
||||
additionalSpace = "0M"; # Defaults to 512M.
|
||||
copyChannel = false;
|
||||
}
|
||||
```
|
||||
|
||||
Some arguments can be left out, they are shown explicitly for the sake of the example.
|
||||
@@ -78,29 +78,36 @@ Building this derivation will provide a QCOW2 disk image containing only the Nix
|
||||
To produce a NixOS installation image disk with UEFI and bootloader installed:
|
||||
```nix
|
||||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
pkgs = import <nixpkgs> { };
|
||||
lib = pkgs.lib;
|
||||
make-disk-image = import <nixpkgs/nixos/lib/make-disk-image.nix>;
|
||||
evalConfig = import <nixpkgs/nixos/lib/eval-config.nix>;
|
||||
in
|
||||
make-disk-image {
|
||||
inherit pkgs lib;
|
||||
inherit (evalConfig {
|
||||
make-disk-image {
|
||||
inherit pkgs lib;
|
||||
inherit
|
||||
(evalConfig {
|
||||
modules = [
|
||||
{
|
||||
fileSystems."/" = { device = "/dev/vda"; fsType = "ext4"; autoFormat = true; };
|
||||
fileSystems."/" = {
|
||||
device = "/dev/vda";
|
||||
fsType = "ext4";
|
||||
autoFormat = true;
|
||||
};
|
||||
boot.grub.device = "/dev/vda";
|
||||
}
|
||||
];
|
||||
}) config;
|
||||
format = "qcow2";
|
||||
onlyNixStore = false;
|
||||
partitionTableType = "legacy+gpt";
|
||||
installBootLoader = true;
|
||||
touchEFIVars = true;
|
||||
diskSize = "auto";
|
||||
additionalSpace = "0M"; # Defaults to 512M.
|
||||
copyChannel = false;
|
||||
memSize = 2048; # Qemu VM memory size in megabytes. Defaults to 1024M.
|
||||
}
|
||||
})
|
||||
config
|
||||
;
|
||||
format = "qcow2";
|
||||
onlyNixStore = false;
|
||||
partitionTableType = "legacy+gpt";
|
||||
installBootLoader = true;
|
||||
touchEFIVars = true;
|
||||
diskSize = "auto";
|
||||
additionalSpace = "0M"; # Defaults to 512M.
|
||||
copyChannel = false;
|
||||
memSize = 2048; # Qemu VM memory size in megabytes. Defaults to 1024M.
|
||||
}
|
||||
```
|
||||
|
||||
@@ -76,7 +76,11 @@ Note that no user namespace is created, which means that you won't be able to ru
|
||||
This example uses `ociTools.buildContainer` to create a simple container that runs `bash`.
|
||||
|
||||
```nix
|
||||
{ ociTools, lib, bash }:
|
||||
{
|
||||
ociTools,
|
||||
lib,
|
||||
bash,
|
||||
}:
|
||||
ociTools.buildContainer {
|
||||
args = [
|
||||
(lib.getExe bash)
|
||||
|
||||
@@ -91,7 +91,12 @@ See [](#ex-portableService-hello) to understand how to use the output of `portab
|
||||
The following example builds a Portable Service image with the `hello` package, along with a service unit that runs it.
|
||||
|
||||
```nix
|
||||
{ lib, writeText, portableService, hello }:
|
||||
{
|
||||
lib,
|
||||
writeText,
|
||||
portableService,
|
||||
hello,
|
||||
}:
|
||||
let
|
||||
hello-service = writeText "hello.service" ''
|
||||
[Unit]
|
||||
@@ -151,7 +156,13 @@ To make things available globally, you must specify the `symlinks` attribute whe
|
||||
The following package builds on the package from [](#ex-portableService-hello) to make `/etc/ssl` available globally (this is only for illustrative purposes, because `hello` doesn't use `/etc/ssl`).
|
||||
|
||||
```nix
|
||||
{ lib, writeText, portableService, hello, cacert }:
|
||||
{
|
||||
lib,
|
||||
writeText,
|
||||
portableService,
|
||||
hello,
|
||||
cacert,
|
||||
}:
|
||||
let
|
||||
hello-service = writeText "hello.service" ''
|
||||
[Unit]
|
||||
@@ -167,7 +178,10 @@ portableService {
|
||||
inherit (hello) version;
|
||||
units = [ hello-service ];
|
||||
symlinks = [
|
||||
{ object = "${cacert}/etc/ssl"; symlink = "/etc/ssl"; }
|
||||
{
|
||||
object = "${cacert}/etc/ssl";
|
||||
symlink = "/etc/ssl";
|
||||
}
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
@@ -26,7 +26,9 @@ To change a normal derivation to a checkpoint based build, these steps must be t
|
||||
|
||||
## Example {#sec-checkpoint-build-example}
|
||||
```nix
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
{
|
||||
pkgs ? import <nixpkgs> { },
|
||||
}:
|
||||
let
|
||||
inherit (pkgs.checkpointBuildTools)
|
||||
prepareCheckpointBuild
|
||||
@@ -39,5 +41,6 @@ let
|
||||
sed -i 's/Hello, world!/Hello, Nix!/g' src/hello.c
|
||||
'';
|
||||
});
|
||||
in mkCheckpointBuild changedHello helloCheckpoint
|
||||
in
|
||||
mkCheckpointBuild changedHello helloCheckpoint
|
||||
```
|
||||
|
||||
@@ -48,12 +48,19 @@ It is useful with functions in `dockerTools` to allow building Docker images tha
|
||||
This example includes the `hello` binary in the image so it can do something besides just have the extra files.
|
||||
|
||||
```nix
|
||||
{ dockerTools, fakeNss, hello }:
|
||||
{
|
||||
dockerTools,
|
||||
fakeNss,
|
||||
hello,
|
||||
}:
|
||||
dockerTools.buildImage {
|
||||
name = "image-with-passwd";
|
||||
tag = "latest";
|
||||
|
||||
copyToRoot = [ fakeNss hello ];
|
||||
copyToRoot = [
|
||||
fakeNss
|
||||
hello
|
||||
];
|
||||
|
||||
config = {
|
||||
Cmd = [ "/bin/hello" ];
|
||||
@@ -70,8 +77,8 @@ The following code uses `override` to add extra lines to `/etc/passwd` and `/etc
|
||||
```nix
|
||||
{ fakeNss }:
|
||||
fakeNss.override {
|
||||
extraPasswdLines = ["newuser:x:9001:9001:new user:/var/empty:/bin/sh"];
|
||||
extraGroupLines = ["newuser:x:9001:"];
|
||||
extraPasswdLines = [ "newuser:x:9001:9001:new user:/var/empty:/bin/sh" ];
|
||||
extraGroupLines = [ "newuser:x:9001:" ];
|
||||
}
|
||||
```
|
||||
:::
|
||||
|
||||
@@ -36,22 +36,29 @@ Accepted arguments are:
|
||||
You can create a simple environment using a `shell.nix` like this:
|
||||
|
||||
```nix
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
{
|
||||
pkgs ? import <nixpkgs> { },
|
||||
}:
|
||||
|
||||
(pkgs.buildFHSEnv {
|
||||
name = "simple-x11-env";
|
||||
targetPkgs = pkgs: (with pkgs; [
|
||||
udev
|
||||
alsa-lib
|
||||
]) ++ (with pkgs.xorg; [
|
||||
libX11
|
||||
libXcursor
|
||||
libXrandr
|
||||
]);
|
||||
multiPkgs = pkgs: (with pkgs; [
|
||||
udev
|
||||
alsa-lib
|
||||
]);
|
||||
targetPkgs =
|
||||
pkgs:
|
||||
(with pkgs; [
|
||||
udev
|
||||
alsa-lib
|
||||
])
|
||||
++ (with pkgs.xorg; [
|
||||
libX11
|
||||
libXcursor
|
||||
libXrandr
|
||||
]);
|
||||
multiPkgs =
|
||||
pkgs:
|
||||
(with pkgs; [
|
||||
udev
|
||||
alsa-lib
|
||||
]);
|
||||
runScript = "bash";
|
||||
}).env
|
||||
```
|
||||
|
||||
@@ -8,11 +8,16 @@ repetition when using it with `nix-shell` (or `nix develop`).
|
||||
Here is a common usage example:
|
||||
|
||||
```nix
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
{
|
||||
pkgs ? import <nixpkgs> { },
|
||||
}:
|
||||
pkgs.mkShell {
|
||||
packages = [ pkgs.gnumake ];
|
||||
|
||||
inputsFrom = [ pkgs.hello pkgs.gnutar ];
|
||||
inputsFrom = [
|
||||
pkgs.hello
|
||||
pkgs.gnutar
|
||||
];
|
||||
|
||||
shellHook = ''
|
||||
export DEBUG=1
|
||||
|
||||
@@ -31,25 +31,34 @@ If the build fails and Nix is run with the `-K/--keep-failed` option, a script `
|
||||
|
||||
Build the derivation hello inside a VM:
|
||||
```nix
|
||||
{ pkgs }: with pkgs; with vmTools;
|
||||
runInLinuxVM hello
|
||||
{ pkgs }: with pkgs; with vmTools; runInLinuxVM hello
|
||||
```
|
||||
|
||||
Build inside a VM with extra memory:
|
||||
```nix
|
||||
{ pkgs }: with pkgs; with vmTools;
|
||||
runInLinuxVM (hello.overrideAttrs (_: { memSize = 1024; }))
|
||||
{ pkgs }:
|
||||
with pkgs;
|
||||
with vmTools;
|
||||
runInLinuxVM (
|
||||
hello.overrideAttrs (_: {
|
||||
memSize = 1024;
|
||||
})
|
||||
)
|
||||
```
|
||||
|
||||
Use VM with a disk image (implicitly sets `diskImage`, see [`vmTools.createEmptyImage`](#vm-tools-createEmptyImage)):
|
||||
```nix
|
||||
{ pkgs }: with pkgs; with vmTools;
|
||||
runInLinuxVM (hello.overrideAttrs (_: {
|
||||
preVM = createEmptyImage {
|
||||
size = 1024;
|
||||
fullName = "vm-image";
|
||||
};
|
||||
}))
|
||||
{ pkgs }:
|
||||
with pkgs;
|
||||
with vmTools;
|
||||
runInLinuxVM (
|
||||
hello.overrideAttrs (_: {
|
||||
preVM = createEmptyImage {
|
||||
size = 1024;
|
||||
fullName = "vm-image";
|
||||
};
|
||||
})
|
||||
)
|
||||
```
|
||||
|
||||
## `vmTools.extractFs` {#vm-tools-extractFs}
|
||||
@@ -66,8 +75,7 @@ Takes a file, such as an ISO, and extracts its contents into the store.
|
||||
|
||||
Extract the contents of an ISO file:
|
||||
```nix
|
||||
{ pkgs }: with pkgs; with vmTools;
|
||||
extractFs { file = ./image.iso; }
|
||||
{ pkgs }: with pkgs; with vmTools; extractFs { file = ./image.iso; }
|
||||
```
|
||||
|
||||
## `vmTools.extractMTDfs` {#vm-tools-extractMTDfs}
|
||||
@@ -86,14 +94,12 @@ Generate a script that can be used to run an interactive session in the given im
|
||||
|
||||
Create a script for running a Fedora 27 VM:
|
||||
```nix
|
||||
{ pkgs }: with pkgs; with vmTools;
|
||||
makeImageTestScript diskImages.fedora27x86_64
|
||||
{ pkgs }: with pkgs; with vmTools; makeImageTestScript diskImages.fedora27x86_64
|
||||
```
|
||||
|
||||
Create a script for running an Ubuntu 20.04 VM:
|
||||
```nix
|
||||
{ pkgs }: with pkgs; with vmTools;
|
||||
makeImageTestScript diskImages.ubuntu2004x86_64
|
||||
{ pkgs }: with pkgs; with vmTools; makeImageTestScript diskImages.ubuntu2004x86_64
|
||||
```
|
||||
|
||||
## `vmTools.diskImageFuns` {#vm-tools-diskImageFuns}
|
||||
@@ -137,8 +143,13 @@ A set of functions that build a predefined set of minimal Linux distributions im
|
||||
|
||||
8GiB image containing Firefox in addition to the default packages:
|
||||
```nix
|
||||
{ pkgs }: with pkgs; with vmTools;
|
||||
diskImageFuns.ubuntu2004x86_64 { extraPackages = [ "firefox" ]; size = 8192; }
|
||||
{ pkgs }:
|
||||
with pkgs;
|
||||
with vmTools;
|
||||
diskImageFuns.ubuntu2004x86_64 {
|
||||
extraPackages = [ "firefox" ];
|
||||
size = 8192;
|
||||
}
|
||||
```
|
||||
|
||||
## `vmTools.diskImageExtraFuns` {#vm-tools-diskImageExtraFuns}
|
||||
|
||||
@@ -98,7 +98,8 @@ It has two modes:
|
||||
```nix
|
||||
{
|
||||
"https://nix\\.dev/manual/nix/[a-z0-9.-]*" = "${nix.doc}/share/doc/nix/manual";
|
||||
"https://nixos\\.org/manual/nix/(un)?stable" = "${emptyDirectory}/placeholder-to-disallow-old-nix-docs-urls";
|
||||
"https://nixos\\.org/manual/nix/(un)?stable" =
|
||||
"${emptyDirectory}/placeholder-to-disallow-old-nix-docs-urls";
|
||||
}
|
||||
```
|
||||
|
||||
@@ -302,18 +303,22 @@ While `testBuildFailure` is designed to keep changes to the original builder's e
|
||||
# Check that a build fails, and verify the changes made during build
|
||||
|
||||
```nix
|
||||
runCommand "example" {
|
||||
failed = testers.testBuildFailure (runCommand "fail" {} ''
|
||||
echo ok-ish >$out
|
||||
echo failing though
|
||||
exit 3
|
||||
'');
|
||||
} ''
|
||||
grep -F 'ok-ish' $failed/result
|
||||
grep -F 'failing though' $failed/testBuildFailure.log
|
||||
[[ 3 = $(cat $failed/testBuildFailure.exit) ]]
|
||||
touch $out
|
||||
''
|
||||
runCommand "example"
|
||||
{
|
||||
failed = testers.testBuildFailure (
|
||||
runCommand "fail" { } ''
|
||||
echo ok-ish >$out
|
||||
echo failing though
|
||||
exit 3
|
||||
''
|
||||
);
|
||||
}
|
||||
''
|
||||
grep -F 'ok-ish' $failed/result
|
||||
grep -F 'failing though' $failed/testBuildFailure.log
|
||||
[[ 3 = $(cat $failed/testBuildFailure.exit) ]]
|
||||
touch $out
|
||||
''
|
||||
```
|
||||
|
||||
:::
|
||||
@@ -396,15 +401,18 @@ testers.testEqualContents {
|
||||
expected = writeText "expected" ''
|
||||
foo baz baz
|
||||
'';
|
||||
actual = runCommand "actual" {
|
||||
# not really necessary for a package that's in stdenv
|
||||
nativeBuildInputs = [ gnused ];
|
||||
base = writeText "base" ''
|
||||
foo bar baz
|
||||
'';
|
||||
} ''
|
||||
sed -e 's/bar/baz/g' $base >$out
|
||||
'';
|
||||
actual =
|
||||
runCommand "actual"
|
||||
{
|
||||
# not really necessary for a package that's in stdenv
|
||||
nativeBuildInputs = [ gnused ];
|
||||
base = writeText "base" ''
|
||||
foo bar baz
|
||||
'';
|
||||
}
|
||||
''
|
||||
sed -e 's/bar/baz/g' $base >$out
|
||||
'';
|
||||
}
|
||||
```
|
||||
|
||||
@@ -515,10 +523,11 @@ Otherwise, the build log explains the difference via `nix-diff`.
|
||||
# Check that two packages produce the same derivation
|
||||
|
||||
```nix
|
||||
testers.testEqualDerivation
|
||||
"The hello package must stay the same when enabling checks."
|
||||
hello
|
||||
(hello.overrideAttrs(o: { doCheck = true; }))
|
||||
testers.testEqualDerivation "The hello package must stay the same when enabling checks." hello (
|
||||
hello.overrideAttrs (o: {
|
||||
doCheck = true;
|
||||
})
|
||||
)
|
||||
```
|
||||
|
||||
:::
|
||||
@@ -586,7 +595,10 @@ testers.runCommand {
|
||||
curl -o /dev/null https://example.com
|
||||
touch $out
|
||||
'';
|
||||
nativeBuildInputs = with pkgs; [ cacert curl ];
|
||||
nativeBuildInputs = with pkgs; [
|
||||
cacert
|
||||
curl
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
@@ -603,15 +615,20 @@ If your test is part of the Nixpkgs repository, or if you need a more general en
|
||||
# Run a NixOS test using `runNixOSTest`
|
||||
|
||||
```nix
|
||||
pkgs.testers.runNixOSTest ({ lib, ... }: {
|
||||
name = "hello";
|
||||
nodes.machine = { pkgs, ... }: {
|
||||
environment.systemPackages = [ pkgs.hello ];
|
||||
};
|
||||
testScript = ''
|
||||
machine.succeed("hello")
|
||||
'';
|
||||
})
|
||||
pkgs.testers.runNixOSTest (
|
||||
{ lib, ... }:
|
||||
{
|
||||
name = "hello";
|
||||
nodes.machine =
|
||||
{ pkgs, ... }:
|
||||
{
|
||||
environment.systemPackages = [ pkgs.hello ];
|
||||
};
|
||||
testScript = ''
|
||||
machine.succeed("hello")
|
||||
'';
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
:::
|
||||
@@ -634,10 +651,17 @@ A [NixOS VM test network](https://nixos.org/nixos/manual/index.html#sec-nixos-te
|
||||
{
|
||||
name = "my-test";
|
||||
nodes = {
|
||||
machine1 = { lib, pkgs, nodes, ... }: {
|
||||
environment.systemPackages = [ pkgs.hello ];
|
||||
services.foo.enable = true;
|
||||
};
|
||||
machine1 =
|
||||
{
|
||||
lib,
|
||||
pkgs,
|
||||
nodes,
|
||||
...
|
||||
}:
|
||||
{
|
||||
environment.systemPackages = [ pkgs.hello ];
|
||||
services.foo.enable = true;
|
||||
};
|
||||
# machine2 = ...;
|
||||
};
|
||||
testScript = ''
|
||||
|
||||
@@ -66,15 +66,17 @@ runCommandWith :: {
|
||||
# Invocation of `runCommandWith`
|
||||
|
||||
```nix
|
||||
runCommandWith {
|
||||
name = "example";
|
||||
derivationArgs.nativeBuildInputs = [ cowsay ];
|
||||
} ''
|
||||
cowsay > $out <<EOMOO
|
||||
'runCommandWith' is a bit cumbersome,
|
||||
so we have more ergonomic wrappers.
|
||||
EOMOO
|
||||
''
|
||||
runCommandWith
|
||||
{
|
||||
name = "example";
|
||||
derivationArgs.nativeBuildInputs = [ cowsay ];
|
||||
}
|
||||
''
|
||||
cowsay > $out <<EOMOO
|
||||
'runCommandWith' is a bit cumbersome,
|
||||
so we have more ergonomic wrappers.
|
||||
EOMOO
|
||||
''
|
||||
```
|
||||
|
||||
:::
|
||||
@@ -118,7 +120,7 @@ While the type signature(s) differ from [`runCommandWith`], individual arguments
|
||||
# Invocation of `runCommand`
|
||||
|
||||
```nix
|
||||
runCommand "my-example" {} ''
|
||||
runCommand "my-example" { } ''
|
||||
echo My example command is running
|
||||
|
||||
mkdir $out
|
||||
@@ -238,7 +240,7 @@ The following fields are either required, are of a different type than in the sp
|
||||
Write a desktop file `/nix/store/<store path>/my-program.desktop` to the Nix store.
|
||||
|
||||
```nix
|
||||
{makeDesktopItem}:
|
||||
{ makeDesktopItem }:
|
||||
makeDesktopItem {
|
||||
name = "my-program";
|
||||
desktopName = "My Program";
|
||||
@@ -260,7 +262,10 @@ makeDesktopItem {
|
||||
mimeTypes = [ "video/mp4" ];
|
||||
categories = [ "Utility" ];
|
||||
implements = [ "org.my-program" ];
|
||||
keywords = [ "Video" "Player" ];
|
||||
keywords = [
|
||||
"Video"
|
||||
"Player"
|
||||
];
|
||||
startupNotify = false;
|
||||
startupWMClass = "MyProgram";
|
||||
prefersNonDefaultGPU = false;
|
||||
@@ -276,18 +281,22 @@ makeDesktopItem {
|
||||
Override the `hello` package to add a desktop item.
|
||||
|
||||
```nix
|
||||
{ copyDesktopItems
|
||||
, hello
|
||||
, makeDesktopItem }:
|
||||
{
|
||||
copyDesktopItems,
|
||||
hello,
|
||||
makeDesktopItem,
|
||||
}:
|
||||
|
||||
hello.overrideAttrs {
|
||||
nativeBuildInputs = [ copyDesktopItems ];
|
||||
|
||||
desktopItems = [(makeDesktopItem {
|
||||
name = "hello";
|
||||
desktopName = "Hello";
|
||||
exec = "hello";
|
||||
})];
|
||||
desktopItems = [
|
||||
(makeDesktopItem {
|
||||
name = "hello";
|
||||
desktopName = "Hello";
|
||||
exec = "hello";
|
||||
})
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
@@ -446,10 +455,9 @@ The store path will include the name, and it will be a file.
|
||||
Write the string `Contents of File` to `/nix/store/<store path>`:
|
||||
|
||||
```nix
|
||||
writeText "my-file"
|
||||
''
|
||||
writeText "my-file" ''
|
||||
Contents of File
|
||||
''
|
||||
''
|
||||
```
|
||||
:::
|
||||
|
||||
@@ -486,10 +494,9 @@ The store path will be a directory.
|
||||
Write the string `Contents of File` to `/nix/store/<store path>/share/my-file`:
|
||||
|
||||
```nix
|
||||
writeTextDir "share/my-file"
|
||||
''
|
||||
writeTextDir "share/my-file" ''
|
||||
Contents of File
|
||||
''
|
||||
''
|
||||
```
|
||||
:::
|
||||
|
||||
@@ -528,10 +535,9 @@ The store path will include the name, and it will be a file.
|
||||
Write the string `Contents of File` to `/nix/store/<store path>` and make the file executable.
|
||||
|
||||
```nix
|
||||
writeScript "my-file"
|
||||
''
|
||||
writeScript "my-file" ''
|
||||
Contents of File
|
||||
''
|
||||
''
|
||||
```
|
||||
|
||||
This is equivalent to:
|
||||
@@ -570,10 +576,9 @@ The store path will include the name, and it will be a directory.
|
||||
# Usage of `writeScriptBin`
|
||||
|
||||
```nix
|
||||
writeScriptBin "my-script"
|
||||
''
|
||||
writeScriptBin "my-script" ''
|
||||
echo "hi"
|
||||
''
|
||||
''
|
||||
```
|
||||
:::
|
||||
|
||||
@@ -614,10 +619,9 @@ This function is almost exactly like [](#trivial-builder-writeScript), except th
|
||||
# Usage of `writeShellScript`
|
||||
|
||||
```nix
|
||||
writeShellScript "my-script"
|
||||
''
|
||||
writeShellScript "my-script" ''
|
||||
echo "hi"
|
||||
''
|
||||
''
|
||||
```
|
||||
:::
|
||||
|
||||
@@ -657,10 +661,9 @@ This function is a combination of [](#trivial-builder-writeShellScript) and [](#
|
||||
# Usage of `writeShellScriptBin`
|
||||
|
||||
```nix
|
||||
writeShellScriptBin "my-script"
|
||||
''
|
||||
writeShellScriptBin "my-script" ''
|
||||
echo "hi"
|
||||
''
|
||||
''
|
||||
```
|
||||
:::
|
||||
|
||||
@@ -685,26 +688,40 @@ These functions concatenate `files` to the Nix store in a single file. This is u
|
||||
|
||||
Here are a few examples:
|
||||
```nix
|
||||
|
||||
# Writes my-file to /nix/store/<store path>
|
||||
concatTextFile {
|
||||
name = "my-file";
|
||||
files = [ drv1 "${drv2}/path/to/file" ];
|
||||
}
|
||||
# See also the `concatText` helper function below.
|
||||
concatTextFile
|
||||
{
|
||||
name = "my-file";
|
||||
files = [
|
||||
drv1
|
||||
"${drv2}/path/to/file"
|
||||
];
|
||||
}
|
||||
# See also the `concatText` helper function below.
|
||||
|
||||
# Writes executable my-file to /nix/store/<store path>/bin/my-file
|
||||
concatTextFile {
|
||||
name = "my-file";
|
||||
files = [ drv1 "${drv2}/path/to/file" ];
|
||||
executable = true;
|
||||
destination = "/bin/my-file";
|
||||
}
|
||||
# Writes contents of files to /nix/store/<store path>
|
||||
concatText "my-file" [ file1 file2 ]
|
||||
# Writes executable my-file to /nix/store/<store path>/bin/my-file
|
||||
concatTextFile
|
||||
{
|
||||
name = "my-file";
|
||||
files = [
|
||||
drv1
|
||||
"${drv2}/path/to/file"
|
||||
];
|
||||
executable = true;
|
||||
destination = "/bin/my-file";
|
||||
}
|
||||
# Writes contents of files to /nix/store/<store path>
|
||||
concatText
|
||||
"my-file"
|
||||
[ file1 file2 ]
|
||||
|
||||
# Writes contents of files to /nix/store/<store path>
|
||||
concatScript "my-file" [ file1 file2 ]
|
||||
# Writes contents of files to /nix/store/<store path>
|
||||
concatScript
|
||||
"my-file"
|
||||
[
|
||||
file1
|
||||
file2
|
||||
]
|
||||
```
|
||||
|
||||
## `writeShellApplication` {#trivial-builder-writeShellApplication}
|
||||
@@ -722,7 +739,10 @@ For example, the following shell application can refer to `curl` directly, rathe
|
||||
writeShellApplication {
|
||||
name = "show-nixos-org";
|
||||
|
||||
runtimeInputs = [ curl w3m ];
|
||||
runtimeInputs = [
|
||||
curl
|
||||
w3m
|
||||
];
|
||||
|
||||
text = ''
|
||||
curl -s 'https://nixos.org' | w3m -dump -T text/html
|
||||
@@ -736,7 +756,14 @@ This can be used to put many derivations into the same directory structure. It w
|
||||
Here is an example:
|
||||
```nix
|
||||
# adds symlinks of hello and stack to current build and prints "links added"
|
||||
symlinkJoin { name = "myexample"; paths = [ pkgs.hello pkgs.stack ]; postBuild = "echo links added"; }
|
||||
symlinkJoin {
|
||||
name = "myexample";
|
||||
paths = [
|
||||
pkgs.hello
|
||||
pkgs.stack
|
||||
];
|
||||
postBuild = "echo links added";
|
||||
}
|
||||
```
|
||||
This creates a derivation with a directory structure like the following:
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user