From a7404783f812c573e18661a60b7eb399bbcc4188 Mon Sep 17 00:00:00 2001 From: Malo Bourgon Date: Wed, 28 Jun 2023 16:24:31 -0700 Subject: [PATCH] Add support for multiple GHC versions --- constants.nix | 5 +++ flake.nix | 88 +++++++++++++++++++++++++++++++-------------------- stack.nix | 9 ++++-- stack.yaml | 1 + 4 files changed, 66 insertions(+), 37 deletions(-) create mode 100644 constants.nix diff --git a/constants.nix b/constants.nix new file mode 100644 index 0000000..42cc5e4 --- /dev/null +++ b/constants.nix @@ -0,0 +1,5 @@ +{ + name = "prefmanager"; + ghcVersions = map (v: "ghc${v}") [ "8107" "902" "928" "945" "962" ]; + defaultGhcVersion = "ghc928"; +} diff --git a/flake.nix b/flake.nix index b05483e..37769bf 100644 --- a/flake.nix +++ b/flake.nix @@ -10,43 +10,63 @@ devshell.inputs.nixpkgs.follows = "nixpkgs"; }; - outputs = { self, devshell, nixpkgs, flake-utils, plist-source, ... }: { - overlays.prefmanager = _: prev: { - inherit (self.packages.${prev.stdenv.system}) prefmanager; - }; - } // flake-utils.lib.eachSystem [ "x86_64-darwin" "aarch64-darwin" ] (system: + outputs = { self, devshell, nixpkgs, flake-utils, plist-source, ... }: let - pkgs = nixpkgs.legacyPackages.${system}; - compiler = pkgs.haskell.packages.ghc928; - hlib = pkgs.haskell.lib; - plist = hlib.markUnbroken (hlib.overrideSrc compiler.plist { src = plist-source; }); - prefmanager = compiler.callCabal2nix "prefmanager" ./. { inherit plist; }; - inherit (devshell.legacyPackages.${system}) mkShell; + inherit (import ./constants.nix) name ghcVersions defaultGhcVersion; in { - # Built by `nix build .` - packages.default = prefmanager; - packages.prefmanager = prefmanager; + overlays.${name} = final: prev: { + haskell = prev.haskell // { + packageOverrides = final.lib.composeExtensions + prev.haskell.packageOverrides + (hfinal: hprev: { + plist = final.haskell.lib.markUnbroken (final.haskell.lib.overrideSrc hprev.plist { + src = plist-source; + }); + ${name} = hfinal.callCabal2nix name ./. { }; + }); + }; - # # This is used by `nix develop .` - devShells.default = mkShell { - name = "prefmanager"; - packages = [ - compiler.haskell-language-server - compiler.implicit-hie - # compiler.weeder - pkgs.stack - pkgs.hlint - ]; - commands = [ - { - help = "Regenerate hie.yaml (run from project root)"; - name = "hie"; - category = "project"; - command = "gen-hie > hie.yaml"; - } - ]; + ${name} = final.haskell.packages.${defaultGhcVersion}.${name}; }; - } - ); + } // flake-utils.lib.eachSystem [ "x86_64-darwin" "aarch64-darwin" ] (system: + let + pkgs = import nixpkgs { inherit system; overlays = [ self.overlays.${name} ]; }; + in + { + packages = { + default = pkgs.${name}; + ${name} = pkgs.${name}; + + # Create package for all `ghcVersions`. + } // builtins.listToAttrs (map + (v: { name = "${name}-${v}"; value = pkgs.haskell.packages.${v}.${name}; }) + ghcVersions + ); + + devShells.default = devshell.legacyPackages.${system}.mkShell { + inherit name; + + packages = builtins.attrValues { + inherit (pkgs.haskell.packages.${defaultGhcVersion}) + haskell-language-server + hlint + implicit-hie + weeder + ; + + inherit (pkgs) stack; + }; + + commands = [ + { + help = "Regenerate hie.yaml (run from project root)"; + name = "hie"; + category = "project"; + command = "gen-hie > hie.yaml"; + } + ]; + }; + } + ); } diff --git a/stack.nix b/stack.nix index c995721..6a8f3cf 100644 --- a/stack.nix +++ b/stack.nix @@ -1,5 +1,8 @@ -with import (import ./default.nix).inputs.nixpkgs {}; +let + inherit (import ./constants.nix) name defaultGhcVersion; + inherit (import (import ./default.nix).inputs.nixpkgs { }) haskell; +in haskell.lib.buildStackProject { - ghc = haskell.compiler.ghc928; - name = "prefmanager"; + ghc = haskell.compiler.${defaultGhcVersion}; + inherit name; } diff --git a/stack.yaml b/stack.yaml index e8c476b..3996d0a 100644 --- a/stack.yaml +++ b/stack.yaml @@ -1,4 +1,5 @@ # GHC 9.2.8 +# Keep in sync with `defaultGhcVersion` in `./constants.nix`. resolver: lts-20.26 extra-deps: