about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2017-06-07T14·17+0200
committerEelco Dolstra <edolstra@gmail.com>2017-06-07T14·17+0200
commitb8283773bd64d7da6859ed520ee19867742a03ba (patch)
tree040443ef177a1a5ec1795fe9bd52d2b050d2fee8 /src
parentc8cc50d46e78de7ae02c2cb7a5159e995c993f61 (diff)
nix: Make all options available as flags
Thus, instead of ‘--option <name> <value>’, you can write ‘--<name>
<value>’. So

  --option http-connections 100

becomes

  --http-connections 100

Apart from brevity, the difference is that it's not an error to set a
non-existent option via --option, but unrecognized arguments are
fatal.

Boolean options have special treatment: they're mapped to the
argument-less flags ‘--<name>’ and ‘--no-<name>’. E.g.

  --option auto-optimise-store false

becomes

  --no-auto-optimise-store
Diffstat (limited to 'src')
-rw-r--r--src/libstore/globals.cc7
-rw-r--r--src/libutil/config.cc25
-rw-r--r--src/libutil/config.hh6
-rw-r--r--src/nix/main.cc2
4 files changed, 40 insertions, 0 deletions
diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc
index 3dd2508a26d3..3f2bea8e7b13 100644
--- a/src/libstore/globals.cc
+++ b/src/libstore/globals.cc
@@ -98,6 +98,13 @@ template<> void BaseSetting<SandboxMode>::toJSON(JSONPlaceholder & out)
     AbstractSetting::toJSON(out);
 }
 
+template<> void BaseSetting<SandboxMode>::convertToArg(Args & args)
+{
+    args.mkFlag(0, name, {}, "Enable sandboxing.", 0, [=](Strings ss) { value = smEnabled; });
+    args.mkFlag(0, "no-" + name, {}, "Disable sandboxing.", 0, [=](Strings ss) { value = smDisabled; });
+    args.mkFlag(0, "relaxed-" + name, {}, "Enable sandboxing, but allow builds to disable it.", 0, [=](Strings ss) { value = smRelaxed; });
+}
+
 void MaxBuildJobsSetting::set(const std::string & str)
 {
     if (str == "auto") value = std::max(1U, std::thread::hardware_concurrency());
diff --git a/src/libutil/config.cc b/src/libutil/config.cc
index f7a46bfee63f..612fb6e68350 100644
--- a/src/libutil/config.cc
+++ b/src/libutil/config.cc
@@ -115,6 +115,13 @@ void Config::toJSON(JSONObject & out)
         }
 }
 
+void Config::convertToArgs(Args & args)
+{
+    for (auto & s : _settings)
+        if (!s.second.isAlias)
+            s.second.setting->convertToArg(args);
+}
+
 AbstractSetting::AbstractSetting(
     const std::string & name,
     const std::string & description,
@@ -128,12 +135,22 @@ void AbstractSetting::toJSON(JSONPlaceholder & out)
     out.write(to_string());
 }
 
+void AbstractSetting::convertToArg(Args & args)
+{
+}
+
 template<typename T>
 void BaseSetting<T>::toJSON(JSONPlaceholder & out)
 {
     out.write(value);
 }
 
+template<typename T>
+void BaseSetting<T>::convertToArg(Args & args)
+{
+    args.mkFlag(0, name, {}, description, 1, [=](Strings ss) { set(*ss.begin()); });
+}
+
 template<> void BaseSetting<std::string>::set(const std::string & str)
 {
     value = str;
@@ -174,6 +191,12 @@ template<> std::string BaseSetting<bool>::to_string()
     return value ? "true" : "false";
 }
 
+template<> void BaseSetting<bool>::convertToArg(Args & args)
+{
+    args.mkFlag(0, name, {}, description, 0, [=](Strings ss) { value = true; });
+    args.mkFlag(0, "no-" + name, {}, description, 0, [=](Strings ss) { value = false; });
+}
+
 template<> void BaseSetting<Strings>::set(const std::string & str)
 {
     value = tokenizeString<Strings>(str);
@@ -216,6 +239,8 @@ template class BaseSetting<long long>;
 template class BaseSetting<unsigned long long>;
 template class BaseSetting<bool>;
 template class BaseSetting<std::string>;
+template class BaseSetting<Strings>;
+template class BaseSetting<StringSet>;
 
 void PathSetting::set(const std::string & str)
 {
diff --git a/src/libutil/config.hh b/src/libutil/config.hh
index 77620d47d371..2ca643fe0e71 100644
--- a/src/libutil/config.hh
+++ b/src/libutil/config.hh
@@ -63,6 +63,8 @@ public:
     void resetOverriden();
 
     void toJSON(JSONObject & out);
+
+    void convertToArgs(Args & args);
 };
 
 class AbstractSetting
@@ -99,6 +101,8 @@ protected:
 
     virtual void toJSON(JSONPlaceholder & out);
 
+    virtual void convertToArg(Args & args);
+
     bool isOverriden() { return overriden; }
 };
 
@@ -132,6 +136,8 @@ public:
 
     std::string to_string() override;
 
+    void convertToArg(Args & args) override;
+
     void toJSON(JSONPlaceholder & out) override;
 };
 
diff --git a/src/nix/main.cc b/src/nix/main.cc
index 216f0bccef11..f83843415e3b 100644
--- a/src/nix/main.cc
+++ b/src/nix/main.cc
@@ -22,6 +22,8 @@ struct NixArgs : virtual MultiCommand, virtual MixCommonArgs
         });
 
         mkFlag(0, "version", "show version information", std::bind(printVersion, programName));
+
+        settings.convertToArgs(*this);
     }
 };