From 1f3c3a3785b21fe349480b562d5f97b8cc9caa6d Mon Sep 17 00:00:00 2001
From: regnat <rg@regnat.ovh>
Date: Fri, 5 Nov 2021 16:17:49 +0100
Subject: [PATCH] Make the flake options work when using the daemon
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When setting flake-local options (with the `nixConfig` field), forward
these options to the daemon in case we’re using one.

This is necessary in particular for options like `binary-caches` or
`post-build-hook` to make sense.

Fix <https://github.com/NixOS/nix/commit/343239fc8a1993f707a990c2cd54a41f1fa3de99#r44356843>
---
 src/libexpr/flake/flake.cc    |  2 +-
 src/libstore/remote-store.cc  |  4 ++++
 src/libstore/remote-store.hh  |  2 ++
 src/libstore/store-api.hh     |  5 +++++
 tests/flake-local-settings.sh | 35 +++++++++++++++++++++++++++++++++++
 tests/local.mk                |  1 +
 6 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 tests/flake-local-settings.sh

diff --git a/src/libexpr/flake/flake.cc b/src/libexpr/flake/flake.cc
index c9d848495..07ed3caa2 100644
--- a/src/libexpr/flake/flake.cc
+++ b/src/libexpr/flake/flake.cc
@@ -307,7 +307,7 @@ LockedFlake lockFlake(
 
     if (lockFlags.applyNixConfig) {
         flake.config.apply();
-        // FIXME: send new config to the daemon.
+        state.store->setOptions();
     }
 
     try {
diff --git a/src/libstore/remote-store.cc b/src/libstore/remote-store.cc
index 7decc059c..274203f8d 100644
--- a/src/libstore/remote-store.cc
+++ b/src/libstore/remote-store.cc
@@ -290,6 +290,10 @@ ConnectionHandle RemoteStore::getConnection()
     return ConnectionHandle(connections->get());
 }
 
+void RemoteStore::setOptions()
+{
+    setOptions(*(getConnection().handle));
+}
 
 bool RemoteStore::isValidPathUncached(const StorePath & path)
 {
diff --git a/src/libstore/remote-store.hh b/src/libstore/remote-store.hh
index a3036e6b0..5f6da9af5 100644
--- a/src/libstore/remote-store.hh
+++ b/src/libstore/remote-store.hh
@@ -147,6 +147,8 @@ protected:
 
     virtual void setOptions(Connection & conn);
 
+    void setOptions() override;
+
     ConnectionHandle getConnection();
 
     friend struct ConnectionHandle;
diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh
index 7d02340df..8472e726a 100644
--- a/src/libstore/store-api.hh
+++ b/src/libstore/store-api.hh
@@ -724,6 +724,11 @@ public:
     virtual void createUser(const std::string & userName, uid_t userId)
     { }
 
+    /*
+     * Synchronises the options of the client with those of the daemon
+     * (a no-op when there’s no daemon)
+     */
+    virtual void setOptions() { }
 protected:
 
     Stats stats;
diff --git a/tests/flake-local-settings.sh b/tests/flake-local-settings.sh
new file mode 100644
index 000000000..c037431c8
--- /dev/null
+++ b/tests/flake-local-settings.sh
@@ -0,0 +1,35 @@
+source common.sh
+
+clearStore
+rm -rf $TEST_HOME/.cache $TEST_HOME/.config $TEST_HOME/.local
+
+cp ./simple.nix ./simple.builder.sh ./config.nix $TEST_HOME
+
+cd $TEST_HOME
+
+rm -f post-hook-ran
+cat <<EOF > echoing-post-hook.sh
+#!/bin/sh
+
+echo "ThePostHookRan" > $PWD/post-hook-ran
+EOF
+chmod +x echoing-post-hook.sh
+
+cat <<EOF > flake.nix
+{
+    nixConfig.post-build-hook = "$PWD/echoing-post-hook.sh";
+
+    outputs = a: {
+       defaultPackage.$system = import ./simple.nix;
+    };
+}
+EOF
+
+# Ugly hack for testing
+mkdir -p .local/share/nix
+cat <<EOF > .local/share/nix/trusted-settings.json
+{"post-build-hook":{"$PWD/echoing-post-hook.sh":true}}
+EOF
+
+nix build
+test -f post-hook-ran || fail "The post hook should have ran"
diff --git a/tests/local.mk b/tests/local.mk
index 8d454ee19..4c53f09f2 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -46,6 +46,7 @@ nix_tests = \
   recursive.sh \
   describe-stores.sh \
   flakes.sh \
+  flake-local-settings.sh \
   build.sh \
   compute-levels.sh \
   repl.sh \