Skip to content

Commit 3907718

Browse files
eamsdenangerman
authored andcommitted
Add callStackToNix function (#116)
Allow building a stack project directly without needing to generate the scaffolding. ```nix { mkStackPkgSet, callStackToNix, ... }: let pkgSet = mkStackPkgSet { stack-pkgs = callStackToNix { src = ./.; }; pkg-def-extras = []; modules = []; }; in pkgSet.config.hsPkgs ```
1 parent 072debc commit 3907718

File tree

12 files changed

+194
-3
lines changed

12 files changed

+194
-3
lines changed

call-stack-to-nix.nix

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
{ nix-tools, pkgs }:
7+
{ src, stackYaml ? null }:
8+
let
9+
pkgsNix = pkgs.stdenv.mkDerivation {
10+
name = "pkgs-nix";
11+
inherit src;
12+
nativeBuildInputs = [ nix-tools pkgs.nix-prefetch-git ];
13+
installPhase = ''
14+
export LANG=C.utf8 # Needed or stack-to-nix will die on unicode inputs
15+
mkdir -p $out
16+
stack-to-nix --stack-yaml=$src/${if stackYaml == null then "stack.yaml" else stackYaml} -o $out
17+
mv $out/pkgs.nix $out/default.nix
18+
'';
19+
};
20+
in import pkgsNix

default.nix

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,12 @@ let
106106
};
107107

108108
# Programs for generating Nix expressions from Cabal and Stack
109-
# files.
110-
nix-tools = self.callPackage ./nix-tools { inherit fetchExternal; };
109+
# files. We need to make sure we build this from the buildPackages,
110+
# we never want to actually cross compile nix-tools on it's own.
111+
nix-tools = pkgs.buildPackages.callPackage ./nix-tools { inherit fetchExternal; inherit (self) mkCabalProjectPkgSet; };
112+
113+
# Function to call stackToNix
114+
callStackToNix = self.callPackage ./call-stack-to-nix.nix {};
111115

112116
# Snapshots of Hackage and Stackage, converted to Nix expressions,
113117
# regularly updated.

test/callStackToNix/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.stack-work/
2+
stack-simple.cabal
3+
*~

test/callStackToNix/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

test/callStackToNix/app/Main.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Main where
2+
3+
import Lib
4+
5+
main :: IO ()
6+
main = someFunc

test/callStackToNix/default.nix

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{ stdenv, mkStackPkgSet, callStackToNix }:
2+
3+
with stdenv.lib;
4+
5+
let
6+
pkgSet = mkStackPkgSet {
7+
stack-pkgs = callStackToNix { src = ./.; };
8+
pkg-def-extras = [];
9+
modules = [];
10+
};
11+
12+
packages = pkgSet.config.hsPkgs;
13+
14+
in
15+
stdenv.mkDerivation {
16+
name = "callStackToNix-test";
17+
18+
buildCommand = ''
19+
exe="${packages.stack-simple.components.exes.stack-simple-exe}/bin/stack-simple-exe"
20+
21+
printf "checking whether executable runs... " >& 2
22+
$exe
23+
24+
touch $out
25+
'';
26+
27+
meta.platforms = platforms.all;
28+
29+
passthru = {
30+
# Attributes used for debugging with nix repl
31+
inherit pkgSet packages;
32+
};
33+
}

test/callStackToNix/package.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: stack-simple
2+
version: 0.1.0.0
3+
github: "githubuser/stack-simple"
4+
license: BSD3
5+
author: "Author name here"
6+
maintainer: "[email protected]"
7+
copyright: "2019 Author name here"
8+
9+
extra-source-files:
10+
- README.md
11+
- ChangeLog.md
12+
13+
# Metadata used when publishing your package
14+
# synopsis: Short description of your package
15+
# category: Web
16+
17+
# To avoid duplicated efforts in documentation and dealing with the
18+
# complications of embedding Haddock markup inside cabal files, it is
19+
# common to point users to the README.md file.
20+
description: Please see the README on GitHub at <https://github.com/githubuser/stack-simple#readme>
21+
22+
dependencies:
23+
- base >= 4.7 && < 5
24+
25+
library:
26+
source-dirs: src
27+
28+
executables:
29+
stack-simple-exe:
30+
main: Main.hs
31+
source-dirs: app
32+
ghc-options:
33+
- -threaded
34+
- -rtsopts
35+
- -with-rtsopts=-N
36+
dependencies:
37+
- stack-simple
38+
39+
tests:
40+
stack-simple-test:
41+
main: Spec.hs
42+
source-dirs: test
43+
ghc-options:
44+
- -threaded
45+
- -rtsopts
46+
- -with-rtsopts=-N
47+
dependencies:
48+
- stack-simple

test/callStackToNix/src/Lib.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Lib
2+
( someFunc
3+
) where
4+
5+
someFunc :: IO ()
6+
someFunc = putStrLn "someFunc"

test/callStackToNix/stack.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This file was automatically generated by 'stack init'
2+
#
3+
# Some commonly used options have been documented as comments in this file.
4+
# For advanced use and comprehensive documentation of the format, please see:
5+
# https://docs.haskellstack.org/en/stable/yaml_configuration/
6+
7+
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
8+
# A snapshot resolver dictates the compiler version and the set of packages
9+
# to be used for project dependencies. For example:
10+
#
11+
# resolver: lts-3.5
12+
# resolver: nightly-2015-09-21
13+
# resolver: ghc-7.10.2
14+
#
15+
# The location of a snapshot can be provided as a file or url. Stack assumes
16+
# a snapshot provided as a file might change, whereas a url resource does not.
17+
#
18+
# resolver: ./custom-snapshot.yaml
19+
# resolver: https://example.com/snapshots/2018-01-01.yaml
20+
resolver: lts-13.6
21+
22+
# User packages to be built.
23+
# Various formats can be used as shown in the example below.
24+
#
25+
# packages:
26+
# - some-directory
27+
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
28+
# - location:
29+
# git: https://github.com/commercialhaskell/stack.git
30+
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
31+
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
32+
# subdirs:
33+
# - auto-update
34+
# - wai
35+
packages:
36+
- .
37+
# Dependency packages to be pulled from upstream that are not in the resolver
38+
# using the same syntax as the packages field.
39+
# (e.g., acme-missiles-0.3)
40+
extra-deps:
41+
- process-1.6.5.0
42+
- transformers-0.5.6.2
43+
44+
# Override default flag values for local packages and extra-deps
45+
# flags: {}
46+
47+
# Extra package databases containing global packages
48+
# extra-package-dbs: []
49+
50+
# Control whether we use the GHC we find on the path
51+
# system-ghc: true
52+
#
53+
# Require a specific version of stack, using version ranges
54+
# require-stack-version: -any # Default
55+
# require-stack-version: ">=1.9"
56+
#
57+
# Override the architecture used by stack, especially useful on Windows
58+
# arch: i386
59+
# arch: x86_64
60+
#
61+
# Extra directories used by stack for building
62+
# extra-include-dirs: [/path/to/dir]
63+
# extra-lib-dirs: [/path/to/dir]
64+
#
65+
# Allow a newer minor version of GHC than the snapshot specifies
66+
# compiler-check: newer-minor

test/callStackToNix/test/Spec.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main :: IO ()
2+
main = putStrLn "Test suite not yet implemented"

test/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ in {
1515
with-packages = haskell.callPackage ./with-packages { inherit util; };
1616
builder-haddock = haskell.callPackage ./builder-haddock {};
1717
stack-simple = haskell.callPackage ./stack-simple {};
18+
callStackToNix = haskell.callPackage ./callStackToNix {};
1819

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

test/tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ 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-out-link --keep-going ./default.nix
15+
nix build $NIX_BUILD_ARGS --no-link --keep-going -f ./default.nix
1616
echo >& 2
1717

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

0 commit comments

Comments
 (0)