on iko/ghcjs-patch-master help sign in
Fix restricted eval mode issue with mkCacheFile (#392)

If the .nix files listed in the cache are full paths pointing at the store we get errors like this in restricted eval mode: error: access to path '/nix/store/...-cabal-simple.nix' is forbidden in restricted mode

commit43fdc4dafd95b03f78f16775cbabb83b747f64c1 authorHamish Mackenzie <Hamish.Mackenzie@iohk.io> dateTue 7 Jan 2020, 19:07 (7 years ago) parentf2f5c46
8 files changed+65−6
lib/call-stack-to-nix.nix+1−1 overlays/haskell.nix+21−5 test/default.nix+1 test/stack-source-repo/.gitignore+2 test/stack-source-repo/default.nix+15 test/stack-source-repo/package.yaml+8 test/stack-source-repo/src/Lib.hs+6 test/stack-source-repo/stack.yaml+11
lib/call-stack-to-nix.nix+1−1
@@ -35,7 +35,7 @@ let
3535 } (''
3636 mkdir -p $out
3737 '' + pkgs.lib.optionalString (cache != null) ''
38− cp ${mkCacheFile cache} $out/.stack-to-nix.cache
38+ cp ${mkCacheFile cache}/.stack-to-nix.cache* $out
3939 '' + ''
4040 (cd $out && stack-to-nix ${stackToNixArgs})
4141
overlays/haskell.nix+21−5
@@ -285,18 +285,34 @@ self: super: {
285285
286286 sha256String = if isNull sha256 then self.buildPackages.lib.fakeSha256 else sha256;
287287
288− in "${url} ${rev} ${subdir} ${sha256String} ${name} ${nix-expr}";
288+ in {
289+ line = "${url} ${rev} ${subdir} ${sha256String} ${name}";
290+ inherit nix-expr;
291+ };
289292
290293 # Given a list of repos:
291294 # [ { name = ...; url = ...; rev = ...; ref = ...; sha256 = ...; cabal-file = ...; type = ...; is-private = ...; } ]
292295 # produce a cache file that can be used for
293296 # stack-to-nix or plan-to-nix to prevent them
294297 # from needing network access.
298+ # The cache contains only local paths to nix files so that it can
299+ # the results of `stack-to-nix` can be imported in restrected eval
300+ # mode.
295301 mkCacheFile = repos:
296− self.buildPackages.pkgs.writeTextFile {
297− name = "cache-file";
298− text = self.buildPackages.lib.concatMapStringsSep "\n" mkCacheLine repos;
299− };
302+ self.buildPackages.pkgs.runCommand "cache-file" {} ''
303+ mkdir -p $out
304+ touch $out/.stack-to-nix.cache
305+ ${self.lib.concatStrings (
306+ self.lib.lists.zipListsWith (n: repo:
307+ let l = mkCacheLine repo;
308+ in ''
309+ cp ${l.nix-expr} $out/.stack-to-nix.cache.${toString n}
310+ echo ${l.line} .stack-to-nix.cache.${toString n} >> $out/.stack-to-nix.cache
311+ '')
312+ (self.lib.lists.range 0 ((builtins.length repos) - 1))
313+ repos)
314+ }
315+ '';
300316
301317 mkCacheModule = cache:
302318 # for each item in the `cache`, set
test/default.nix+1
@@ -40,6 +40,7 @@ in pkgs.recurseIntoAttrs {
4040 ghc-options-cabal = haskell-nix.callPackage ./ghc-options/cabal.nix {};
4141 ghc-options-stack = haskell-nix.callPackage ./ghc-options/stack.nix {};
4242 exe-only = haskell-nix.callPackage ./exe-only { inherit util; };
43+ stack-source-repo = haskell-nix.callPackage ./stack-source-repo {};
4344
4445 # Run unit tests with: nix-instantiate --eval --strict -A unit.tests
4546 # An empty list means success.
test/stack-source-repo/.gitignorenew file+2
@@ -0,0 +1,2 @@
1+/.stack-work/
2+/*.cabal
test/stack-source-repo/default.nixnew file+15
@@ -0,0 +1,15 @@
1+{ stackProject', recurseIntoAttrs }:
2+
3+let
4+ project = stackProject' {
5+ src = ./.;
6+ cache = [ { is-private = false; name = "cabal-simple"; rev = "bc01ebc05a8105035c9449943046b46c8364b932"; sha256 = "003lm3pm024vhbfmii7xcdd9v2rczpflxf7gdl2pyxia7p014i8z"; subdir = "test/cabal-simple"; url = "https://github.com/input-output-hk/haskell.nix.git"; } ];
7+ };
8+ packages = project.hsPkgs;
9+
10+in recurseIntoAttrs {
11+ ifdInputs = {
12+ inherit (project) stack-nix;
13+ };
14+ inherit (packages.stack-source-repo.components) library;
15+}
test/stack-source-repo/package.yamlnew file+8
@@ -0,0 +1,8 @@
1+name: stack-source-repo
2+
3+dependencies:
4+- base
5+- cabal-simple
6+
7+library:
8+ source-dirs: src
test/stack-source-repo/src/Lib.hsnew file+6
@@ -0,0 +1,6 @@
1+module Lib
2+ ( someFunc
3+ ) where
4+
5+someFunc :: IO ()
6+someFunc = putStrLn "someFunc"
test/stack-source-repo/stack.yamlnew file+11
@@ -0,0 +1,11 @@
1+resolver: lts-14.13
2+
3+packages:
4+- .
5+
6+extra-deps:
7+- git: https://github.com/input-output-hk/haskell.nix.git
8+ commit: bc01ebc05a8105035c9449943046b46c8364b932
9+ subdirs:
10+ - test/cabal-simple
11+