2021-10-25 16:53:01 +03:00
|
|
|
#include "experimental-features.hh"
|
2021-10-26 15:29:48 +03:00
|
|
|
#include "util.hh"
|
|
|
|
|
2021-10-25 16:53:01 +03:00
|
|
|
#include "nlohmann/json.hpp"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2023-04-03 01:11:16 +03:00
|
|
|
struct ExperimentalFeatureDetails
|
|
|
|
{
|
|
|
|
ExperimentalFeature tag;
|
|
|
|
std::string_view name;
|
|
|
|
std::string_view description;
|
2021-10-25 16:53:01 +03:00
|
|
|
};
|
|
|
|
|
2023-04-03 01:11:16 +03:00
|
|
|
constexpr std::array<ExperimentalFeatureDetails, 11> xpFeatureDetails = {{
|
|
|
|
{
|
|
|
|
.tag = Xp::CaDerivations,
|
|
|
|
.name = "ca-derivations",
|
|
|
|
.description = R"(
|
2023-04-06 06:10:11 +03:00
|
|
|
Allow derivations to be content-addressed in order to prevent
|
|
|
|
rebuilds when changes to the derivation do not result in changes to
|
|
|
|
the derivation's output. See
|
2023-04-05 02:16:10 +03:00
|
|
|
[__contentAddressed](@docroot@/language/advanced-attributes.md#adv-attr-__contentAddressed)
|
|
|
|
for details.
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::ImpureDerivations,
|
|
|
|
.name = "impure-derivations",
|
|
|
|
.description = R"(
|
2023-04-06 06:10:11 +03:00
|
|
|
Allow derivations to produce non-fixed outputs by setting the
|
|
|
|
`__impure` derivation attribute to `true`. An impure derivation can
|
|
|
|
have differing outputs each time it is built.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```
|
|
|
|
derivation {
|
|
|
|
name = "impure";
|
|
|
|
builder = /bin/sh;
|
|
|
|
__impure = true; # mark this derivation as impure
|
|
|
|
args = [ "-c" "read -n 10 random < /dev/random; echo $random > $out" ];
|
|
|
|
system = builtins.currentSystem;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Each time this derivation is built, it can produce a different
|
|
|
|
output (as the builder outputs random bytes to `$out`). Impure
|
|
|
|
derivations also have access to the network, and only fixed-output
|
|
|
|
or other impure derivations can rely on impure derivations. Finally,
|
|
|
|
an impure derivation cannot also be
|
|
|
|
[content-addressed](#xp-feature-ca-derivations).
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::Flakes,
|
|
|
|
.name = "flakes",
|
|
|
|
.description = R"(
|
2023-04-06 06:10:11 +03:00
|
|
|
Enable flakes. See the manual entry for [`nix
|
|
|
|
flake`](../command-ref/new-cli/nix3-flake.md) for details.
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::NixCommand,
|
|
|
|
.name = "nix-command",
|
|
|
|
.description = R"(
|
2023-04-05 02:16:10 +03:00
|
|
|
Enable the new `nix` subcommands. See the manual on
|
|
|
|
[`nix`](@docroot@/command-ref/new-cli/nix.md) for details.
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::RecursiveNix,
|
|
|
|
.name = "recursive-nix",
|
|
|
|
.description = R"(
|
2023-04-06 06:10:11 +03:00
|
|
|
Allow derivation builders to call Nix, and thus build derivations
|
|
|
|
recursively.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```
|
|
|
|
with import <nixpkgs> {};
|
|
|
|
|
|
|
|
runCommand "foo"
|
|
|
|
{
|
|
|
|
buildInputs = [ nix jq ];
|
|
|
|
NIX_PATH = "nixpkgs=${<nixpkgs>}";
|
|
|
|
}
|
|
|
|
''
|
|
|
|
hello=$(nix-build -E '(import <nixpkgs> {}).hello.overrideDerivation (args: { name = "recursive-hello"; })')
|
|
|
|
|
|
|
|
mkdir -p $out/bin
|
|
|
|
ln -s $hello/bin/hello $out/bin/hello
|
|
|
|
''
|
|
|
|
```
|
|
|
|
|
|
|
|
An important restriction on recursive builders is disallowing
|
|
|
|
arbitrary substitutions. For example, running
|
|
|
|
|
|
|
|
```
|
|
|
|
nix-store -r /nix/store/kmwd1hq55akdb9sc7l3finr175dajlby-hello-2.10
|
|
|
|
```
|
|
|
|
|
|
|
|
in the above `runCommand` script would be disallowed, as this could
|
|
|
|
lead to derivations with hidden dependencies or breaking
|
|
|
|
reproducibility by relying on the current state of the Nix store. An
|
|
|
|
exception would be if
|
|
|
|
`/nix/store/kmwd1hq55akdb9sc7l3finr175dajlby-hello-2.10` were
|
|
|
|
already in the build inputs or built by a previous recursive Nix
|
|
|
|
call.
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::NoUrlLiterals,
|
|
|
|
.name = "no-url-literals",
|
|
|
|
.description = R"(
|
2023-04-06 06:10:11 +03:00
|
|
|
Disallow unquoted URLs as part of the Nix language syntax. The Nix
|
|
|
|
language allows for URL literals, like so:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ nix repl
|
|
|
|
Welcome to Nix 2.15.0. Type :? for help.
|
|
|
|
|
|
|
|
nix-repl> http://foo
|
|
|
|
"http://foo"
|
|
|
|
```
|
|
|
|
|
|
|
|
But enabling this experimental feature will cause the Nix parser to
|
|
|
|
throw an error when encountering a URL literal:
|
|
|
|
|
|
|
|
```
|
|
|
|
$ nix repl --extra-experimental-features 'no-url-literals'
|
|
|
|
Welcome to Nix 2.15.0. Type :? for help.
|
|
|
|
|
|
|
|
nix-repl> http://foo
|
|
|
|
error: URL literals are disabled
|
|
|
|
|
|
|
|
at «string»:1:1:
|
|
|
|
|
|
|
|
1| http://bar
|
|
|
|
| ^
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
While this is currently an experimental feature, unquoted URLs are
|
|
|
|
being deprecated and their usage is discouraged.
|
|
|
|
|
|
|
|
The reason is that, as opposed to path literals, URLs have no
|
|
|
|
special properties that distinguish them from regular strings, URLs
|
|
|
|
containing parameters have to be quoted anyway, and unquoted URLs
|
|
|
|
may confuse external tooling.
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::FetchClosure,
|
|
|
|
.name = "fetch-closure",
|
|
|
|
.description = R"(
|
2023-04-05 02:16:10 +03:00
|
|
|
Enable the use of the [`fetchClosure`](@docroot@/language/builtins.md#builtins-fetchClosure) built-in function in the Nix language.
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::ReplFlake,
|
|
|
|
.name = "repl-flake",
|
|
|
|
.description = R"(
|
2023-04-05 02:16:10 +03:00
|
|
|
Allow passing [installables](@docroot@/command-ref/new-cli/nix.md#installables) to `nix repl`, making its interface consistent with the other experimental commands.
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::AutoAllocateUids,
|
|
|
|
.name = "auto-allocate-uids",
|
|
|
|
.description = R"(
|
|
|
|
Allows Nix to automatically pick UIDs for builds, rather than creating
|
2023-04-05 02:16:10 +03:00
|
|
|
`nixbld*` user accounts. See the [`auto-allocate-uids`](#conf-auto-allocate-uids) setting for details.
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::Cgroups,
|
|
|
|
.name = "cgroups",
|
|
|
|
.description = R"(
|
|
|
|
Allows Nix to execute builds inside cgroups. See
|
2023-04-05 02:16:10 +03:00
|
|
|
the [`use-cgroups`](#conf-use-cgroups) setting for details.
|
2023-04-03 01:11:16 +03:00
|
|
|
)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.tag = Xp::DiscardReferences,
|
|
|
|
.name = "discard-references",
|
|
|
|
.description = R"(
|
2023-04-05 02:16:10 +03:00
|
|
|
Allow the use of the [`unsafeDiscardReferences`](@docroot@/language/advanced-attributes.html#adv-attr-unsafeDiscardReferences) attribute in derivations
|
|
|
|
that use [structured attributes](@docroot@/language/advanced-attributes.html#adv-attr-structuredAttrs). This disables scanning of outputs for
|
2023-04-03 01:11:16 +03:00
|
|
|
runtime dependencies.
|
|
|
|
)",
|
|
|
|
},
|
|
|
|
}};
|
|
|
|
|
|
|
|
static_assert(
|
|
|
|
[]() constexpr {
|
|
|
|
for (auto [index, feature] : enumerate(xpFeatureDetails))
|
|
|
|
if (index != (size_t)feature.tag)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}(),
|
|
|
|
"array order does not match enum tag order");
|
|
|
|
|
2021-10-25 16:53:01 +03:00
|
|
|
const std::optional<ExperimentalFeature> parseExperimentalFeature(const std::string_view & name)
|
|
|
|
{
|
|
|
|
using ReverseXpMap = std::map<std::string_view, ExperimentalFeature>;
|
2021-10-26 15:29:48 +03:00
|
|
|
|
2023-04-03 01:11:16 +03:00
|
|
|
static std::unique_ptr<ReverseXpMap> reverseXpMap = [](){
|
2021-10-26 15:29:48 +03:00
|
|
|
auto reverseXpMap = std::make_unique<ReverseXpMap>();
|
2023-04-03 01:11:16 +03:00
|
|
|
for (auto & xpFeature : xpFeatureDetails)
|
|
|
|
(*reverseXpMap)[xpFeature.name] = xpFeature.tag;
|
2021-10-26 15:29:48 +03:00
|
|
|
return reverseXpMap;
|
|
|
|
}();
|
2021-10-25 16:53:01 +03:00
|
|
|
|
2021-10-26 15:29:48 +03:00
|
|
|
if (auto feature = get(*reverseXpMap, name))
|
|
|
|
return *feature;
|
|
|
|
else
|
2021-10-25 16:53:01 +03:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2023-04-03 01:11:16 +03:00
|
|
|
std::string_view showExperimentalFeature(const ExperimentalFeature tag)
|
2021-10-25 16:53:01 +03:00
|
|
|
{
|
2023-04-03 01:11:16 +03:00
|
|
|
assert((size_t)tag < xpFeatureDetails.size());
|
|
|
|
return xpFeatureDetails[(size_t)tag].name;
|
2023-03-28 03:12:49 +03:00
|
|
|
}
|
|
|
|
|
2023-04-05 05:57:11 +03:00
|
|
|
nlohmann::json documentExperimentalFeatures() {
|
|
|
|
StringMap res;
|
|
|
|
for (auto & xpFeature : xpFeatureDetails)
|
|
|
|
res[std::string { xpFeature.name }] =
|
|
|
|
trim(stripIndentation(xpFeature.description));
|
|
|
|
return (nlohmann::json) res;
|
2021-10-25 16:53:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::set<ExperimentalFeature> parseFeatures(const std::set<std::string> & rawFeatures)
|
|
|
|
{
|
|
|
|
std::set<ExperimentalFeature> res;
|
2023-04-03 01:11:16 +03:00
|
|
|
for (auto & rawFeature : rawFeatures)
|
2021-10-25 16:53:01 +03:00
|
|
|
if (auto feature = parseExperimentalFeature(rawFeature))
|
|
|
|
res.insert(*feature);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
MissingExperimentalFeature::MissingExperimentalFeature(ExperimentalFeature feature)
|
|
|
|
: Error("experimental Nix feature '%1%' is disabled; use '--extra-experimental-features %1%' to override", showExperimentalFeature(feature))
|
|
|
|
, missingFeature(feature)
|
2021-10-26 15:29:48 +03:00
|
|
|
{}
|
2021-10-25 16:53:01 +03:00
|
|
|
|
|
|
|
std::ostream & operator <<(std::ostream & str, const ExperimentalFeature & feature)
|
|
|
|
{
|
|
|
|
return str << showExperimentalFeature(feature);
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:37:28 +03:00
|
|
|
void to_json(nlohmann::json & j, const ExperimentalFeature & feature)
|
|
|
|
{
|
2022-04-20 16:41:01 +03:00
|
|
|
j = showExperimentalFeature(feature);
|
|
|
|
}
|
|
|
|
|
2022-05-03 15:37:28 +03:00
|
|
|
void from_json(const nlohmann::json & j, ExperimentalFeature & feature)
|
|
|
|
{
|
2022-04-20 16:41:01 +03:00
|
|
|
const std::string input = j;
|
|
|
|
const auto parsed = parseExperimentalFeature(input);
|
|
|
|
|
|
|
|
if (parsed.has_value())
|
|
|
|
feature = *parsed;
|
|
|
|
else
|
|
|
|
throw Error("Unknown experimental feature '%s' in JSON input", input);
|
|
|
|
}
|
|
|
|
|
2021-10-25 16:53:01 +03:00
|
|
|
}
|