Skip to content

Commit

Permalink
Test with Cabal and Stack, fix build with older snapshots
Browse files Browse the repository at this point in the history
1. Fix code in order to be compatible with older Stack LTS and ‘nixpkgs’
   snapshots

2. Add few older ‘nixpkgs’ release version Niv pins and add ‘shell.nix’ argument
   to be able to simply use different version
   (e.g. ‘--argstr nixpkgs-release 19.04’)

3. Update Stack LTS snapshot to the latest stable one (17.6, GHC 8.10.4)

4. Add some code to ‘shell.nix’ for testing with older Stack LTS snapshots

5. Provide an example of how to test the application with older Stack LTS
   snapshot in the comments of ‘stack.yaml’
  • Loading branch information
unclechu committed Mar 20, 2021
1 parent 6a89f26 commit c911e72
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
build
dist
dist-newstyle
result

cabal.sandbox.config
Expand Down
36 changes: 36 additions & 0 deletions nix/sources.json
Expand Up @@ -34,5 +34,41 @@
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/a3fa481cb683d619ab9e1a64877f3c0c5fd24f40.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs-19.03": {
"branch": "release-19.03",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "27aaaa5ba6923104a23bae75d323dc975390195e",
"sha256": "1fdzc6psj369bi9bybgm9vwl5c7sa4z6k2sdz95ypcpgcg3s12ql",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/27aaaa5ba6923104a23bae75d323dc975390195e.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs-19.09": {
"branch": "release-19.09",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "75f4ba05c63be3f147bcc2f7bd4ba1f029cedcb1",
"sha256": "157c64220lf825ll4c0cxsdwg7cxqdx4z559fdp7kpz0g6p8fhhr",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/75f4ba05c63be3f147bcc2f7bd4ba1f029cedcb1.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"nixpkgs-20.03": {
"branch": "release-20.03",
"description": "Nix Packages collection",
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "462c6fe4b115804ea4d5bee7103c0f46ff9f9cfb",
"sha256": "0a4bdaxjrds6kyw9s3r2vbc96f7glgy5n03jk4zr5z8h3j4v2s81",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/462c6fe4b115804ea4d5bee7103c0f46ff9f9cfb.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}
44 changes: 41 additions & 3 deletions shell.nix
Expand Up @@ -2,9 +2,17 @@
# License: GNU/GPLv3 https://raw.githubusercontent.com/unclechu/place-cursor-at/master/LICENSE

let sources = import nix/sources.nix; in
# Forwarded arguments
args@
{ pkgs ? import sources.nixpkgs {}
{ pkgs ?
import sources."nixpkgs${
if nixpkgs-release == "20.09" then "" else "-${nixpkgs-release}"
}" {}

# Tested with lower 19.03 and upper 20.09 pins.
, nixpkgs-release ? "20.09" # One of: '20.09', '20.03', '19.09', '19.03'

# It’s not provided in 19.03 for instance
, yq-go ? (import sources.nixpkgs { inherit (pkgs) config; }).yq-go

# Forwarded build options
, __src ? null
Expand All @@ -16,6 +24,7 @@ args@
# Local arguments
, withCabal ? false
, withStack ? false
, withStackNixDependencies ? false
, withPackageRepl ? false # Adds package library modules into GHCi REPL
, withHoogle ? true
, buildExecutable ? true
Expand All @@ -28,7 +37,7 @@ let
hp = pkg.haskellPackages;
name = pkg.haskellPackage.pname;

inherit (nix-utils) wrapExecutable;
inherit (nix-utils) esc wrapExecutable;
pkgReplGhc = hp.ghcWithPackages (p: [p.${name}]);

# Produces ‘PACKAGE-NAME-ghc’ and ‘PACKAGE-NAME-ghci’ files.
Expand All @@ -43,6 +52,34 @@ let
(exe "ghci")
(exe "ghc")
];

stackNixDependencies =
let
stackYamlFile = "${./stack.yaml}";
path = "nix.packages";

nixModule = pkgs.runCommand "${name}-stack-yaml-nix-packages" {} ''
set -Eeuo pipefail || exit
JSON=$(${esc yq-go}/bin/yq r -j -- ${esc stackYamlFile} ${esc path})
if [[ -z $JSON ]]; then
>&2 printf 'Failed to extract "%s" from "%s"!\n' \
${esc path} ${esc stackYamlFile}
exit 1
fi
printf '%s' "$JSON" > "$out"
'';

attrPathToDerivation = x:
pkgs.lib.attrsets.getAttrFromPath (pkgs.lib.splitString "." x) pkgs;

attrPaths = builtins.fromJSON (builtins.readFile nixModule);
in
assert builtins.isList attrPaths;
assert builtins.length attrPaths > 0;
assert builtins.all builtins.isString attrPaths;
map attrPathToDerivation attrPaths;
in
hp.shellFor {
packages = p: [
Expand All @@ -54,6 +91,7 @@ hp.shellFor {
buildInputs =
(if withCabal then [ hp.cabal-install ] else []) ++
(if withStack then [ hp.stack ] else []) ++
(if withStackNixDependencies then stackNixDependencies else []) ++
(if buildExecutable then [ hp.${name} ] else []) ++
(if withPackageRepl then pkgRepl else []);
}
9 changes: 8 additions & 1 deletion src/place-cursor-at.hs
Expand Up @@ -9,7 +9,7 @@
{-# LANGUAGE UnicodeSyntax, BangPatterns, MultiWayIf, ViewPatterns, ScopedTypeVariables, GADTs #-}
{-# LANGUAGE LambdaCase, DerivingStrategies, GeneralizedNewtypeDeriving #-}

import Prelude.Unicode
import Prelude.Unicode ((∘), (÷), (≡), (⧺), (∧), (≥))

import Data.Bifunctor (first)
import Data.Bits ((.|.))
Expand Down Expand Up @@ -582,3 +582,10 @@ mkDoneHandler = newEmptyMVar <&> \mvar → DoneApi
(•) = flip (∘)
infixl 9
{-# INLINE (•) #-}

-- This operator is provided by newer version of ‘base-unicode-symbols’.
-- This adds support for older snaphots.
(×) Num a a a a
(×) = (Prelude.*)
infixl 7 ×
{-# INLINE (×) #-}
23 changes: 21 additions & 2 deletions stack.yaml
@@ -1,5 +1,24 @@
# also tested with lts-14.27 (ghc 8.6.5)
resolver: lts-16.6 # ghc 8.8.3
# Also tested with lts-13.19 (ghc 8.6.4).
#
# Test older one using Nix:
# nix-shell \
# --argstr nixpkgs-release 19.03 \
# --arg buildExecutable false \
# --arg withStack true \
# --arg withStackNixDependencies true \
# --run '[[ $(ghc --numeric-version) == 8.6.4 ]] && \
# stack build --system-ghc --no-nix --resolver=lts-13.19 && \
# stack exec --system-ghc --no-nix --resolver=lts-13.19 \
# place-cursor-at'
#
# Test current one using Nix:
# nix-shell \
# --arg buildExecutable false \
# --arg withStack true \
# --run 'stack build --system-ghc && \
# stack exec --system-ghc -- place-cursor-at'
#
resolver: lts-17.6 # ghc 8.10.4

system-ghc: false
packages: [.]
Expand Down
8 changes: 4 additions & 4 deletions stack.yaml.lock
Expand Up @@ -6,7 +6,7 @@
packages: []
snapshots:
- completed:
size: 531718
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/16/6.yaml
sha256: 230a7266fc11f76222bd3bb68e9503ed11d553060a752f164bff6753ed03e271
original: lts-16.6
size: 565712
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/17/6.yaml
sha256: 4e5e581a709c88e3fe26a9ce8bf331435729bead762fb5c190064c6c5bb1b835
original: lts-17.6

0 comments on commit c911e72

Please sign in to comment.