Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/build-helpers/fetchers.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ or

***
```

This function should only be used by non-redistributable software with an unfree license that we need to require the user to download manually.
It produces packages that cannot be built automatically.

## `fetchtorrent` {#fetchtorrent}

`fetchtorrent` expects two arguments. `url` which can either be a Magnet URI (Magnet Link) such as `magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c` or an HTTP URL pointing to a `.torrent` file. It can also take a `config` argument which will craft a `settings.json` configuration file and give it to `transmission`, the underlying program that is performing the fetch. The available config options for `transmission` can be found [here](https://github.com/transmission/transmission/blob/main/docs/Editing-Configuration-Files.md#options)
Expand Down
10 changes: 9 additions & 1 deletion doc/build-helpers/trivial-build-helpers.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Like [`stdenv.mkDerivation`](#sec-using-stdenv), each of these build helpers cre

`runCommand :: String -> AttrSet -> String -> Derivation`

`runCommand name drvAttrs buildCommand` returns a derivation that is built by running the specified shell commands.
The result of `runCommand name drvAttrs buildCommand` is a derivation that is built by running the specified shell commands.

By default `runCommand` runs in a stdenv with no compiler environment, whereas [`runCommandCC`](#trivial-builder-runCommandCC) uses the default stdenv, `pkgs.stdenv`.

`name :: String`
: The name that Nix will append to the store path in the same way that `stdenv.mkDerivation` uses its `name` attribute.
Expand Down Expand Up @@ -153,6 +155,12 @@ Write a text file to the Nix store.

Default: `true`

`derivationArgs` (Attribute set, _optional_)

: Extra arguments to pass to the underlying call to `stdenv.mkDerivation`.

Default: `{}`

The resulting store path will include some variation of the name, and it will be a file unless `destination` is used, in which case it will be a directory.

::: {.example #ex-writeTextFile}
Expand Down
147 changes: 47 additions & 100 deletions pkgs/build-support/trivial-builders/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,24 @@ in

rec {

/*
Run the shell command `buildCommand` to produce a store path named `name`.

The attributes in `env` are added to the environment prior to running the command.
Environment variables set by `stdenv.mkDerivation` take precedence.

By default `runCommand` runs in a stdenv with no compiler environment.
`runCommandCC` uses the default stdenv, `pkgs.stdenv`.

Example:

```nix
runCommand "name" {envVariable = true;} ''echo hello > $out'';
```

```nix
runCommandCC "name" {} ''gcc -o myfile myfile.c; cp myfile $out'';
```

The `*Local` variants force a 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.

It is the same as adding the special fields

```nix
{
preferLocalBuild = true;
allowSubstitutes = false;
}
```

to a derivation’s attributes.
*/
# Docs in doc/build-helpers/trivial-build-helpers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-runCommand
runCommand = name: env: runCommandWith {
stdenv = stdenvNoCC;
runLocal = false;
inherit name;
derivationArgs = env;
};
# Docs in doc/build-helpers/trivial-build-helpers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-runCommandLocal
runCommandLocal = name: env: runCommandWith {
stdenv = stdenvNoCC;
runLocal = true;
inherit name;
derivationArgs = env;
};

# Docs in doc/build-helpers/trivial-build-helpers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-runCommandCC
runCommandCC = name: env: runCommandWith {
stdenv = stdenv;
runLocal = false;
Expand All @@ -67,6 +36,7 @@ rec {
# `runCommandCCLocal` left out on purpose.
# We shouldn’t force the user to have a cc in scope.

# TODO: Move documentation for runCommandWith to the Nixpkgs manual
/*
Generalized version of the `runCommand`-variants
which does customized behavior via a single
Expand Down Expand Up @@ -112,47 +82,18 @@ rec {
// builtins.removeAttrs derivationArgs [ "passAsFile" ]);


/*
Writes a text file to the nix store.
The contents of text is added to the file in the store.

Example:


# Writes my-file to /nix/store/<store path>
writeTextFile {
name = "my-file";
text = ''
Contents of File
'';
}


See also the `writeText` helper function below.


# Writes executable my-file to /nix/store/<store path>/bin/my-file
writeTextFile {
name = "my-file";
text = ''
Contents of File
'';
executable = true;
destination = "/bin/my-file";
}


*/
# Docs in doc/build-helpers/trivial-build-helpers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeTextFile
writeTextFile =
{ name # the name of the derivation
{ name
, text
, executable ? false # run chmod +x ?
, destination ? "" # relative path appended to $out eg "/bin/foo"
, checkPhase ? "" # syntax checks, e.g. for scripts
, executable ? false
, destination ? ""
, checkPhase ? ""
, meta ? { }
, allowSubstitutes ? false
, preferLocalBuild ? true
, derivationArgs ? { } # Extra arguments to pass to `stdenv.mkDerivation`
, derivationArgs ? { }
}:
let
matches = builtins.match "/bin/([^/]+)" destination;
Expand Down Expand Up @@ -240,8 +181,9 @@ rec {
meta.mainProgram = name;
};

# TODO: move parameter documentation to the Nixpkgs manual
# See doc/build-helpers/trivial-build-helpers.chapter.md
# or https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-text-writing
# or https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeShellApplication
writeShellApplication =
{
/*
Expand Down Expand Up @@ -357,6 +299,7 @@ rec {
};

# Create a C binary
# TODO: add to writers? pkgs/build-support/writers
writeCBin = pname: code:
runCommandCC pname
{
Expand All @@ -377,7 +320,9 @@ rec {
$CC -x c code.c -o "$n"
'';


# TODO: deduplicate with documentation in doc/build-helpers/trivial-build-helpers.chapter.md
# see also https://github.com/NixOS/nixpkgs/pull/249721
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-concatText
/* concat a list of files to the nix store.
The contents of files are added to the file in the store.

Expand Down Expand Up @@ -426,7 +371,9 @@ rec {
eval "$checkPhase"
'';


# TODO: deduplicate with documentation in doc/build-helpers/trivial-build-helpers.chapter.md
# see also https://github.com/NixOS/nixpkgs/pull/249721
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-concatText
/*
Writes a text file to nix store with no optional parameters available.

Expand All @@ -440,6 +387,9 @@ rec {
*/
concatText = name: files: concatTextFile { inherit name files; };

# TODO: deduplicate with documentation in doc/build-helpers/trivial-build-helpers.chapter.md
# see also https://github.com/NixOS/nixpkgs/pull/249721
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-concatText
/*
Writes a text file to nix store with and mark it as executable.

Expand All @@ -452,6 +402,10 @@ rec {


/*
TODO: Deduplicate this documentation.
More docs in doc/build-helpers/trivial-build-helpers.chapter.md
See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-symlinkJoin

Create a forest of symlinks to the files in `paths`.

This creates a single derivation that replicates the directory structure
Expand Down Expand Up @@ -528,6 +482,7 @@ rec {
${postBuild}
'';

# TODO: move linkFarm docs to the Nixpkgs manual
/*
Quickly create a set of symlinks to derivations.

Expand Down Expand Up @@ -584,6 +539,7 @@ rec {
${lib.concatStrings linkCommands}
'';

# TODO: move linkFarmFromDrvs docs to the Nixpkgs manual
/*
Easily create a linkFarm from a set of derivations.

Expand All @@ -609,6 +565,7 @@ rec {
let mkEntryFromDrv = drv: { name = drv.name; path = drv; };
in linkFarm name (map mkEntryFromDrv drvs);

# TODO: move onlyBin docs to the Nixpkgs manual
/*
Produce a derivation that links to the target derivation's `/bin`,
and *only* `/bin`.
Expand All @@ -623,7 +580,8 @@ rec {
'';


# docs in doc/builders/special/makesetuphook.section.md
# Docs in doc/builders/special/makesetuphook.section.md
# See https://nixos.org/manual/nixpkgs/unstable/#sec-pkgs.makeSetupHook
makeSetupHook =
{ name ? lib.warn "calling makeSetupHook without passing a name is deprecated." "hook"
, deps ? [ ]
Expand Down Expand Up @@ -665,8 +623,8 @@ rec {
'');


# Write the references (i.e. the runtime dependencies in the Nix store) of `path` to a file.

# Docs in doc/build-helpers/trivial-build-helpers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeReferencesToFile
writeReferencesToFile = path: runCommand "runtime-deps"
Comment on lines +626 to 628
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this going to reflect on NixOS/nix#9054?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can be moved back, and something like nixdoc can be added.
For now what matters is that there's no more duplication and contributors know where to add their docs when they change/add things.

{
exportReferencesGraph = [ "graph" path ];
Expand All @@ -681,11 +639,8 @@ rec {
done < graph
'';

/*
Write the set of references to a file, that is, their immediate dependencies.

This produces the equivalent of `nix-store -q --references`.
*/
# Docs in doc/build-helpers/trivial-build-helpers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeDirectReferencesToFile
writeDirectReferencesToFile = path: runCommand "runtime-references"
{
exportReferencesGraph = [ "graph" path ];
Expand All @@ -710,7 +665,7 @@ rec {
sort ./references >$out
'';


# TODO: move writeStringReferencesToFile docs to the Nixpkgs manual
/*
Extract a string's references to derivations and paths (its
context) and write them to a text file, removing the input string
Expand Down Expand Up @@ -793,21 +748,8 @@ rec {
writeDirectReferencesToFile (writeText "string-file" string);


/* Print an error message if the file with the specified name and
hash doesn't exist in the Nix store. This function should only
be used by non-redistributable software with an unfree license
that we need to require the user to download manually. It produces
packages that cannot be built automatically.

Example:

requireFile {
name = "my-file";
url = "http://example.com/download/";
sha256 = "ffffffffffffffffffffffffffffffffffffffffffffffffffff";
}

*/
# Docs in doc/build-helpers/fetchers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#requirefile
requireFile =
{ name ? null
, sha256 ? null
Expand Down Expand Up @@ -863,6 +805,7 @@ rec {
};


# TODO: move copyPathToStore docs to the Nixpkgs manual
/*
Copy a path to the Nix store.
Nix automatically copies files to the store before stringifying paths.
Expand All @@ -872,11 +815,13 @@ rec {
copyPathToStore = builtins.filterSource (p: t: true);


# TODO: move copyPathsToStore docs to the Nixpkgs manual
/*
Copy a list of paths to the Nix store.
*/
copyPathsToStore = builtins.map copyPathToStore;

# TODO: move applyPatches docs to the Nixpkgs manual
/* Applies a list of patches to a source directory.

Example:
Expand Down Expand Up @@ -922,6 +867,7 @@ rec {
// (optionalAttrs (src?meta) { inherit (src) meta; })
// (removeAttrs args [ "src" "name" "patches" "prePatch" "postPatch" ]));

# TODO: move docs to Nixpkgs manual
/* An immutable file in the store with a length of 0 bytes. */
emptyFile = runCommand "empty-file"
{
Expand All @@ -931,6 +877,7 @@ rec {
preferLocalBuild = true;
} "touch $out";

# TODO: move docs to Nixpkgs manual
/* An immutable empty directory in the store. */
emptyDirectory = runCommand "empty-directory"
{
Expand Down