From 1127faaba2353a1c4dbff70bdf3fc07e6ac92873 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar <3998+srid@users.noreply.github.com> Date: Mon, 30 May 2022 09:16:38 -0400 Subject: [PATCH] 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 * 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 --- flake.lock | 16 +++++++++++ flake.nix | 82 +++++++++++------------------------------------------- 2 files changed, 33 insertions(+), 65 deletions(-) diff --git a/flake.lock b/flake.lock index de37881..2d2d399 100644 --- a/flake.lock +++ b/flake.lock @@ -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" } } diff --git a/flake.nix b/flake.nix index 377cd5c..29bede4 100644 --- a/flake.nix +++ b/flake.nix @@ -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,75 +7,27 @@ 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 "" -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; }; }; + }; }; }