From 79c7d6205c2f06cb2f0c00ea5cdfbdad5b7befa4 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 16 Feb 2023 15:16:30 +0100 Subject: [PATCH] Support unit prefixes in configuration settings E.g. you can now say `--min-free 1G`. --- doc/manual/src/command-ref/conf-file-prefix.md | 7 +++++++ src/libutil/config-impl.hh | 7 ++++--- tests/functional/config.sh | 7 ++++++- tests/functional/gc-auto.sh | 4 ++-- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/doc/manual/src/command-ref/conf-file-prefix.md b/doc/manual/src/command-ref/conf-file-prefix.md index 1e4085977..627806cfb 100644 --- a/doc/manual/src/command-ref/conf-file-prefix.md +++ b/doc/manual/src/command-ref/conf-file-prefix.md @@ -66,5 +66,12 @@ Configuration options can be set on the command line, overriding the values set The `extra-` prefix is supported for settings that take a list of items (e.g. `--extra-trusted users alice` or `--option extra-trusted-users alice`). +## Integer settings + +Settings that have an integer type support the suffixes `K`, `M`, `G` +and `T`. These cause the specified value to be multiplied by 2^10, +2^20, 2^30 and 2^40, respectively. For instance, `--min-free 1M` is +equivalent to `--min-free 1048576`. + # Available settings diff --git a/src/libutil/config-impl.hh b/src/libutil/config-impl.hh index 1da0cb638..1d349fab5 100644 --- a/src/libutil/config-impl.hh +++ b/src/libutil/config-impl.hh @@ -116,10 +116,11 @@ T BaseSetting::parse(const std::string & str) const { static_assert(std::is_integral::value, "Integer required."); - if (auto n = string2Int(str)) - return *n; - else + try { + return string2IntWithUnitPrefix(str); + } catch (...) { throw UsageError("setting '%s' has invalid value '%s'", name, str); + } } template diff --git a/tests/functional/config.sh b/tests/functional/config.sh index efdf2a958..efdafa8ca 100644 --- a/tests/functional/config.sh +++ b/tests/functional/config.sh @@ -66,4 +66,9 @@ exp_features=$(nix config show | grep '^experimental-features' | cut -d '=' -f 2 # Test that it's possible to retrieve a single setting's value val=$(nix config show | grep '^warn-dirty' | cut -d '=' -f 2 | xargs) val2=$(nix config show warn-dirty) -[[ $val == $val2 ]] \ No newline at end of file +[[ $val == $val2 ]] + +# Test unit prefixes. +[[ $(nix config show --min-free 64K min-free) = 65536 ]] +[[ $(nix config show --min-free 1M min-free) = 1048576 ]] +[[ $(nix config show --min-free 2G min-free) = 2147483648 ]] diff --git a/tests/functional/gc-auto.sh b/tests/functional/gc-auto.sh index 521d9e539..281eef20d 100644 --- a/tests/functional/gc-auto.sh +++ b/tests/functional/gc-auto.sh @@ -62,11 +62,11 @@ EOF ) nix build --impure -v -o $TEST_ROOT/result-A -L --expr "$expr" \ - --min-free 1000 --max-free 2000 --min-free-check-interval 1 & + --min-free 1K --max-free 2K --min-free-check-interval 1 & pid1=$! nix build --impure -v -o $TEST_ROOT/result-B -L --expr "$expr2" \ - --min-free 1000 --max-free 2000 --min-free-check-interval 1 & + --min-free 1K --max-free 2K --min-free-check-interval 1 & pid2=$! # Once the first build is done, unblock the second one.