Delegate most Nix to srid/haskell-flake (a flake-parts module for Haskell dev) (#22)

* Move Haskell stuff into separate module

cf. https://github.com/hercules-ci/flake-parts/blob/main/modules/apps.nix

* Parametrize the haskell part

* Allow user to override default attrs

* Add baseBuildTools (so they can be overriden)

* Don't hardcode non-essential build tools

* Avoid types.anything

* Merge into one buildTools, using sets

* getExe and type are redundant

Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>

* Use docstring wherever possible

* Use ${self} as default for root

* Allow defining multiple Haskell packages

The downside: the user is forced to define 'default' flake outputs.

* Automatically create 'default' attr if a singleton projects set

* Make haskell.nix its own repo

Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
This commit is contained in:
Sridhar Ratnakumar 2022-05-30 09:16:38 -04:00 committed by GitHub
parent 3fc6858830
commit 1127faaba2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 65 deletions

16
flake.lock generated
View file

@ -36,6 +36,21 @@
"type": "github"
}
},
"haskell-flake": {
"locked": {
"lastModified": 1653916420,
"narHash": "sha256-RQSbdhZCeDqZkYJN+3g5tSh+TmW2cXXadX7yGNuajg8=",
"owner": "srid",
"repo": "haskell-flake",
"rev": "812ca2cdbc7be6026541125da09ec7e7971b76ec",
"type": "github"
},
"original": {
"owner": "srid",
"repo": "haskell-flake",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1653326962,
@ -56,6 +71,7 @@
"inputs": {
"flake-compat": "flake-compat",
"flake-parts": "flake-parts",
"haskell-flake": "haskell-flake",
"nixpkgs": "nixpkgs"
}
}

View file

@ -1,5 +1,5 @@
{
description = "haskell-template's description";
description = "srid/haskell-template: Nix template for Haskell projects";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
@ -7,74 +7,26 @@
flake-compat.url = "github:edolstra/flake-compat";
flake-compat.flake = false;
flake-compat.inputs.nixpkgs.follows = "nixpkgs";
haskell-flake.url = "github:srid/haskell-flake";
};
# We use flake-parts as a way to make flakes 'system-aware'
# cf. https://github.com/NixOS/nix/issues/3843#issuecomment-661720562
outputs = { self, nixpkgs, flake-parts, ... }:
outputs = { self, nixpkgs, flake-parts, haskell-flake, ... }:
flake-parts.lib.mkFlake { inherit self; } {
systems = nixpkgs.lib.systems.flakeExposed;
# The primed versions (self', inputs') are same as the non-primed
# versions, but with 'system' already applied.
perSystem = { self', inputs', pkgs, system, ... }:
let
inherit (pkgs.lib.lists) optionals;
# Specify GHC version here. To get the appropriate value, run:
# nix-env -f "<nixpkgs>" -qaP -A haskell.compiler
hp = pkgs.haskellPackages; # Eg: pkgs.haskell.packages.ghc921;
# Specify your build/dev dependencies here.
shellDeps = with hp; [
cabal-fmt
cabal-install
ghcid
haskell-language-server
fourmolu
hlint
pkgs.nixpkgs-fmt
pkgs.treefmt
imports = [
haskell-flake.flakeModule
];
project =
{ returnShellEnv ? false
, withHoogle ? false
}:
hp.developPackage {
inherit returnShellEnv withHoogle;
name = "haskell-template";
root = ./.;
overrides = self: super: with pkgs.haskell.lib; {
# Use callCabal2nix to override Haskell dependencies here
# cf. https://tek.brick.do/K3VXJd8mEKO7
# Example:
# > NanoID = self.callCabal2nix "NanoID" inputs'.NanoID { };
# Assumes that you have the 'NanoID' flake input defined.
perSystem = { self', pkgs, ... }: {
haskellProjects.haskell-template = {
buildTools = hp: {
# TODO: Use https://github.com/numtide/treefmt/pull/169
inherit (pkgs)
treefmt
nixpkgs-fmt;
inherit (hp)
cabal-fmt
fourmolu;
};
modifier = drv:
pkgs.haskell.lib.overrideCabal drv (oa: {
# All the Cabal-specific overrides go here.
# For examples on what is possible, see:
# https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/haskell-modules/lib/compose.nix
buildTools = (oa.buildTools or [ ]) ++ optionals returnShellEnv shellDeps;
});
};
in
{
# Used by `nix build ...`
packages = {
default = project { };
};
# Used by `nix run ...`
apps = {
default = {
type = "app";
program = pkgs.lib.getExe self'.packages.default;
};
};
# Used by `nix develop ...`
devShells = {
default = project { returnShellEnv = true; withHoogle = true; };
};
};
};