Skip to content

Commit 6be8866

Browse files
hamishmackangerman
authored andcommitted
Cross comp. & callCabalProjectToNix/callStackToNix (#202)
* Cross comp. & callCabalProjectToNix/callStackToNix This fixes an issue where the these IFD functions fail because they try to build and run a cross compiled nix-tools on the build system. It may also help if there are cross compilation issues with setup-depends (they also access the buildPackages). * Align stack-to-nix ifd with plan-to-nix ifd This also fixes the restricted/pure eval issues with callStackToNix. I've also made the tests run in restricted mode to catch these kinds of issues.
1 parent 234e9e0 commit 6be8866

File tree

16 files changed

+96
-45
lines changed

16 files changed

+96
-45
lines changed

call-stack-to-nix.nix

Lines changed: 0 additions & 20 deletions
This file was deleted.

default.nix

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ let
6262
};
6363
stackage = import stackageSrc;
6464

65-
packages = self: ({
65+
packages = pkgs: self: (rec {
66+
inherit pkgs; # Make pkgs available (it is the underlying nixpkgs)
67+
68+
# Packages built to run on the build platform, not the host platform
69+
buildPackages = pkgs.buildPackages.lib.makeScope pkgs.buildPackages.newScope
70+
(packages pkgs.buildPackages);
71+
6672
# Utility functions for working with the component builder.
6773
haskellLib = let hl = import ./lib { inherit (pkgs) lib; haskellLib = hl; }; in hl;
6874

@@ -125,17 +131,20 @@ let
125131
haskellPackages = self.snapshots."lts-13.26";
126132

127133
# Programs for generating Nix expressions from Cabal and Stack
128-
# files. We need to make sure we build this from the buildPackages,
129-
# we never want to actually cross compile nix-tools on it's own.
130-
nix-tools = pkgs.buildPackages.callPackage ./nix-tools {
134+
# files. This version of nix-tools may be cross compiled.
135+
# We probably never want to actually cross compile nix-tools on
136+
# it's own.
137+
nix-tools-cross-compiled = pkgs.callPackage ./nix-tools {
131138
inherit fetchExternal cleanSourceHaskell;
132-
hpack = pkgs.buildPackages.haskell.lib.justStaticExecutables
133-
(pkgs.buildPackages.haskellPackages.hpack);
139+
hpack = pkgs.haskell.lib.justStaticExecutables
140+
(pkgs.haskellPackages.hpack);
134141
inherit (self) mkCabalProjectPkgSet;
135142
};
136-
137-
# Function to call stackToNix
138-
callStackToNix = self.callPackage ./call-stack-to-nix.nix {};
143+
# While `nix-tools-cross-compiled` may be cross compiled,
144+
# getting it from `buildPackages` we should get
145+
# nix-tools suitable for running on the build system.
146+
nix-tools = buildPackages.nix-tools-cross-compiled;
147+
# TODO perhaps there is a cleaner way to get a suitable nix-tools.
139148

140149
# Snapshots of Hackage and Stackage, converted to Nix expressions,
141150
# regularly updated.
@@ -180,15 +189,22 @@ let
180189

181190
update-index-state-hashes = self.callPackage ./scripts/update-index-state-hashes.nix {};
182191

192+
# Function to call stackToNix
193+
callStackToNix = import ./lib/call-stack-to-nix.nix {
194+
pkgs = buildPackages.pkgs;
195+
inherit (buildPackages.pkgs) runCommand;
196+
inherit (buildPackages) nix-tools;
197+
};
198+
183199
# Takes a haskell src directory runs cabal new-configure and plan-to-nix.
184200
# Resulting nix files are added to nix-plan subdirectory.
185-
callCabalProjectToNix = import ./lib/cabalProjectToNix.nix {
201+
callCabalProjectToNix = import ./lib/call-cabal-project-to-nix.nix {
186202
index-state-hashes = import indexStateHashesPath;
187-
inherit (self) dotCabal;
188-
inherit pkgs;
189-
inherit (pkgs) runCommand cabal-install ghc symlinkJoin cacert;
190-
inherit (pkgs.haskellPackages) hpack;
191-
inherit (self) nix-tools;
203+
inherit (buildPackages) dotCabal;
204+
pkgs = buildPackages.pkgs; # buildPackages;
205+
inherit (buildPackages.pkgs.haskellPackages) hpack;
206+
inherit (buildPackages.pkgs) runCommand cabal-install ghc symlinkJoin cacert;
207+
inherit (buildPackages) nix-tools;
192208
};
193209

194210
# References to the unpacked sources, for caching in a Hydra jobset.
@@ -198,4 +214,4 @@ let
198214
});
199215

200216
in
201-
pkgs.lib.makeScope pkgs.newScope packages
217+
pkgs.lib.makeScope pkgs.newScope (packages pkgs)

lib/cabalProjectToNix.nix renamed to lib/call-cabal-project-to-nix.nix

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let
1717
type == "directory" ||
1818
pkgs.lib.any (i: (pkgs.lib.hasSuffix i path)) [ ".project" ".cabal" "package.yaml" ];
1919
};
20-
plan = runCommand "plan" {
20+
plan = runCommand "plan-to-nix-pkgs" {
2121
nativeBuildInputs = [ nix-tools ghc hpack cabal-install pkgs.rsync pkgs.git ];
2222
} ''
2323
tmp=$(mktemp -d)
@@ -31,7 +31,9 @@ let
3131
find . -name package.yaml -exec hpack "{}" \;
3232
export SSL_CERT_FILE=${cacert}/etc/ssl/certs/ca-bundle.crt
3333
export GIT_SSL_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt
34-
HOME=${dotCabal { inherit index-state; sha256 = index-sha256; }} cabal new-configure
34+
HOME=${dotCabal { inherit index-state; sha256 = index-sha256; }} cabal new-configure \
35+
--with-ghc=${ghc.targetPrefix}ghc \
36+
--with-ghc-pkg=${ghc.targetPrefix}ghc-pkg
3537
3638
export LANG=C.utf8 # Needed or stack-to-nix will die on unicode inputs
3739
mkdir -p $out
@@ -76,7 +78,7 @@ in
7678
# # todo: should we clean `src` to drop any .git, .nix, ... other irelevant files?
7779
# buildInputs = [ plan src ];
7880
# }
79-
runCommand "plan-and-src" { nativeBuildInputs = [ pkgs.rsync ]; } ''
81+
runCommand "plan-to-nix-pkgs-with-src" { nativeBuildInputs = [ pkgs.rsync ]; } ''
8082
mkdir $out
8183
# todo: should we clean `src` to drop any .git, .nix, ... other irelevant files?
8284
rsync -a "${src}/" "$out/"

lib/call-stack-to-nix.nix

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* The function obtained when this is applied to a package set calls
2+
* the stack-to-nix tool on a supplied source set and then
3+
* imports the resulting pkgs.nix. The application of this function
4+
* to a source path can thus be used directly as the input to mkStackPackageSet
5+
*
6+
* see also `call-cabal-project-to-nix`!
7+
*/
8+
{ runCommand, nix-tools, pkgs }:
9+
{ src, stackYaml ? null }:
10+
let
11+
stack = runCommand "stack-to-nix-pkgs" {
12+
nativeBuildInputs = [ nix-tools pkgs.nix-prefetch-git ];
13+
} ''
14+
export LANG=C.utf8 # Needed or stack-to-nix will die on unicode inputs
15+
mkdir -p $out
16+
17+
(cd $out && stack-to-nix --stack-yaml=${src}/${if stackYaml == null then "stack.yaml" else stackYaml} -o .)
18+
19+
# We need to strip out any references to $src, as those won't
20+
# be accessable in restricted mode.
21+
for nixf in $(find $out -name "*.nix" -type f); do
22+
substituteInPlace $nixf --replace "${src}" "."
23+
done
24+
25+
# move pkgs.nix to default.nix ensure we can just nix `import` the result.
26+
mv $out/pkgs.nix $out/default.nix
27+
'';
28+
in
29+
runCommand "stack-to-nix-pkgs-with-src" { nativeBuildInputs = [ pkgs.rsync ]; } ''
30+
mkdir $out
31+
# todo: should we clean `src` to drop any .git, .nix, ... other irelevant files?
32+
rsync -a "${src}/" "$out/"
33+
rsync -a ${stack}/ $out/
34+
# Rsync will have made $out read only and that can cause problems when
35+
# nix sandboxing is enabled (since it can prevent nix from moving the directory
36+
# out of the chroot sandbox).
37+
chmod +w $out
38+
''

nixpkgs/hackage-src.json

Whitespace-only changes.

shell.nix

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
with import <nixpkgs> {};
2+
let this = import ./. {}; in
3+
stdenv.mkDerivation rec {
4+
name = "env";
5+
env = buildEnv { name = name; paths = buildInputs; };
6+
buildInputs = [
7+
this.ghc
8+
this.cabal-install
9+
this.nix-tools
10+
];
11+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/callStackToNix/default.nix renamed to test/call-stack-to-nix/default.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ with stdenv.lib;
44

55
let
66
pkgSet = mkStackPkgSet {
7-
stack-pkgs = callStackToNix { src = ./.; };
7+
stack-pkgs = import (callStackToNix {
8+
src = ../stack-simple;
9+
});
810
pkg-def-extras = [];
911
modules = [];
1012
};
11-
1213
packages = pkgSet.config.hsPkgs;
13-
1414
in
1515
stdenv.mkDerivation {
1616
name = "callStackToNix-test";
File renamed without changes.
File renamed without changes.

test/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ in pkgs.recurseIntoAttrs {
1818
stack-simple = haskell.callPackage ./stack-simple {};
1919
snapshots = haskell.callPackage ./snapshots {};
2020
shell-for = haskell.callPackage ./shell-for {};
21-
# callStackToNix = haskell.callPackage ./callStackToNix {};
22-
# callCabalProjectToNix = haskell.callPackage ./call-cabal-project-to-nix {};
21+
callStackToNix = haskell.callPackage ./call-stack-to-nix {};
22+
callCabalProjectToNix = haskell.callPackage ./call-cabal-project-to-nix {};
2323

2424
# Run unit tests with: nix-instantiate --eval --strict -A unit.tests
2525
# An empty list means success.

test/tests.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ rm -rvf */cabal.project.local */.ghc.environment* */dist */dist-newstyle */.stac
1212
echo >& 2
1313

1414
printf "*** Running the nix-build tests...\n" >& 2
15-
nix build $NIX_BUILD_ARGS --no-link --keep-going -f ./default.nix
15+
nix build $NIX_BUILD_ARGS \
16+
-I . -I .. \
17+
--option restrict-eval true \
18+
--option allowed-uris "https://github.com/NixOS https://github.com/input-output-hk" \
19+
--no-link --keep-going -f default.nix
1620
echo >& 2
1721

1822
printf "*** Running the unit tests... " >& 2

0 commit comments

Comments
 (0)