diff --git a/doc/builders/fetchers.chapter.md b/doc/builders/fetchers.chapter.md index 5b28b2dcb39..28388ba685d 100644 --- a/doc/builders/fetchers.chapter.md +++ b/doc/builders/fetchers.chapter.md @@ -40,6 +40,24 @@ Used with Git. Expects `url` to a Git repo, `rev`, and `sha256`. `rev` in this c Additionally the following optional arguments can be given: `fetchSubmodules = true` makes `fetchgit` also fetch the submodules of a repository. If `deepClone` is set to true, the entire repository is cloned as opposing to just creating a shallow clone. `deepClone = true` also implies `leaveDotGit = true` which means that the `.git` directory of the clone won't be removed after checkout. +If only parts of the repository are needed, `sparseCheckout` can be used. This will prevent git from fetching unnecessary blobs from server, see [git sparse-checkout](https://git-scm.com/docs/git-sparse-checkout) and [git clone --filter](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---filterltfilter-specgt) for more infomation: + +```nix +{ stdenv, fetchgit }: + +stdenv.mkDerivation { + name = "hello"; + src = fetchgit { + url = "https://..."; + sparseCheckout = '' + path/to/be/included + another/path + ''; + sha256 = "0000000000000000000000000000000000000000000000000000"; + }; +} +``` + ## `fetchfossil` {#fetchfossil} Used with Fossil. Expects `url` to a Fossil archive, `rev`, and `sha256`. diff --git a/lib/default.nix b/lib/default.nix index 2dfe62e82a8..26842253880 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -122,8 +122,9 @@ let mkRenamedOptionModule mkMergedOptionModule mkChangedOptionModule mkAliasOptionModule mkDerivedConfig doRename; inherit (self.options) isOption mkEnableOption mkSinkUndeclaredOptions - mergeDefaultOption mergeOneOption mergeEqualOption getValues - getFiles optionAttrSetToDocList optionAttrSetToDocList' + mergeDefaultOption mergeOneOption mergeEqualOption mergeUniqueOption + getValues getFiles + optionAttrSetToDocList optionAttrSetToDocList' scrubOptionValue literalExpression literalExample literalDocBook showOption showFiles unknownModule mkOption; inherit (self.types) isType setType defaultTypeMerge defaultFunctor diff --git a/lib/options.nix b/lib/options.nix index 794ca5e3394..627aac24d2f 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -172,11 +172,13 @@ rec { else if all isInt list && all (x: x == head list) list then head list else throw "Cannot merge definitions of `${showOption loc}'. Definition values:${showDefs defs}"; - mergeOneOption = loc: defs: - if defs == [] then abort "This case should never happen." - else if length defs != 1 then - throw "The unique option `${showOption loc}' is defined multiple times. Definition values:${showDefs defs}" - else (head defs).value; + mergeOneOption = mergeUniqueOption { message = ""; }; + + mergeUniqueOption = { message }: loc: defs: + if length defs == 1 + then (head defs).value + else assert length defs > 1; + throw "The option `${showOption loc}' is defined multiple times.\n${message}\nDefinition values:${showDefs defs}"; /* "Merge" option definitions by checking that they all have the same value. */ mergeEqualOption = loc: defs: diff --git a/lib/types.nix b/lib/types.nix index cc3ac5fdf6f..f2f9b2bca98 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -32,7 +32,6 @@ let last length tail - unique ; inherit (lib.attrsets) attrNames @@ -48,6 +47,7 @@ let mergeDefaultOption mergeEqualOption mergeOneOption + mergeUniqueOption showFiles showOption ; @@ -470,6 +470,18 @@ rec { nestedTypes.elemType = elemType; }; + unique = { message }: type: mkOptionType rec { + name = "unique"; + inherit (type) description check; + merge = mergeUniqueOption { inherit message; }; + emptyValue = type.emptyValue; + getSubOptions = type.getSubOptions; + getSubModules = type.getSubModules; + substSubModules = m: uniq (type.substSubModules m); + functor = (defaultFunctor name) // { wrapped = type; }; + nestedTypes.elemType = type; + }; + # Null or value of ... nullOr = elemType: mkOptionType rec { name = "nullOr"; @@ -599,6 +611,7 @@ rec { # A value from a set of allowed ones. enum = values: let + inherit (lib.lists) unique; show = v: if builtins.isString v then ''"${v}"'' else if builtins.isInt v then builtins.toString v diff --git a/nixos/doc/manual/development/option-types.section.md b/nixos/doc/manual/development/option-types.section.md index ed557206659..56ffa8e9d79 100644 --- a/nixos/doc/manual/development/option-types.section.md +++ b/nixos/doc/manual/development/option-types.section.md @@ -250,6 +250,12 @@ Composed types are types that take a type as parameter. `listOf : Ensures that type *`t`* cannot be merged. It is used to ensure option definitions are declared only once. +`types.unique` `{ message = m }` *`t`* + +: Ensures that type *`t`* cannot be merged. Prints the message *`m`*, after + the line `The option