Skip to content

Commit

Permalink
Delegate most Nix to srid/haskell-flake (a flake-parts module for…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
srid and roberth authored May 30, 2022
1 parent 3fc6858 commit 1127faa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 65 deletions.
16 changes: 16 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 17 additions & 65 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,81 +1,33 @@
{
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";
flake-parts.inputs.nixpkgs.follows = "nixpkgs";
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
];

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.
};
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; };
imports = [
haskell-flake.flakeModule
];
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;
};
};
};
};
}

0 comments on commit 1127faa

Please sign in to comment.