Have packages option auto-detect single-package projects (#75)

Co-authored-by: Ag <aravindmallapureddy@juspay.in>
Co-authored-by: Sridhar Ratnakumar <srid@srid.ca>
This commit is contained in:
ACreed 2023-02-10 07:19:45 +05:30 committed by GitHub
parent 026acf572f
commit 7019678d8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 12 deletions

View File

@ -4,6 +4,7 @@
- New features
- #63: Add `config.haskellProjects.${name}.outputs` containing all flake outputs for that project.
- #49: The `packages` option now autodiscovers the top-level `.cabal` file as its default value.
- API changes
- #37: Group `buildTools` (renamed to `tools`), `hlsCheck` and `hlintCheck` under the `devShell` submodule option; and allow disabling them all using `devShell.enable = false;` (useful if you want haskell-flake to produce just the package outputs).
- #64: Remove hlintCheck (use [treefmt-nix](https://github.com/numtide/treefmt-nix#flake-parts) instead)

View File

@ -10,10 +10,7 @@
imports = [ inputs.haskell-flake.flakeModule ];
perSystem = { self', pkgs, ... }: {
haskellProjects.default = {
packages = {
# You can add more than one local package here.
example.root = ./.; # Assumes ./example.cabal
};
# packages.example.root = ./.; # This value is detected based on .cabal files
# overrides = self: super: { };
# devShell = {
# enable = true; # Enabled by default

View File

@ -114,12 +114,53 @@ in
description = ''
Attrset of local packages in the project repository.
Autodetected by default by looking for `.cabal` files in sub-directories.
Autodiscovered by default by looking for `.cabal` files in
top-level or sub-directories.
'';
default =
# We look for a single *.cabal in project root. Otherwise,
# look for multiple */*.cabal. Otherwise, error out.
#
# In future, we could just read `cabal.project`. See #76.
let
toplevel-cabal-paths =
lib.concatMapAttrs
(f: _:
if lib.strings.hasSuffix ".cabal" f
then { "${lib.strings.removeSuffix ''.cabal'' f}" = self; }
else { }
)
(builtins.readDir self);
subdir-cabal-paths = lib.filesystem.haskellPathsInDir self;
errorNoDefault = msg:
lib.asserts.assertMsg false ''
A default value for `packages` cannot be auto-detected:
${msg}
You must manually specify the `packages` option.
'';
cabal-paths =
if toplevel-cabal-paths != { }
then
let cabalNames = lib.attrNames toplevel-cabal-paths;
in if builtins.length cabalNames > 1
then
errorNoDefault ''
More than one .cabal file found in project root:
- ${lib.concatStringsSep ".cabal\n - " cabalNames}.cabal
''
else
toplevel-cabal-paths
else if subdir-cabal-paths != { }
then
subdir-cabal-paths
else
errorNoDefault "No .cabal file found.";
in
lib.mapAttrs
(_: value: { root = value; })
(lib.filesystem.haskellPathsInDir self);
cabal-paths;
defaultText = lib.literalMD "autodiscovered by reading `self` files.";
};
devShell = mkOption {

View File

@ -13,9 +13,17 @@ else
fi
FLAKE=$(pwd)
cd ./test
# First, build the flake.
# A Nix bug causes incorrect self when in a sub-flake.
# https://github.com/NixOS/nix/issues/7263
# Workaround: copy ./test somewhere outside of this Git repo.
TESTDIR=$(mktemp -d)
trap 'rm -fr "$TESTDIR"' EXIT
cp -r ./test/* "$TESTDIR"
cd "$TESTDIR"
pwd
# First, build the flake
logHeader "Testing nix build"
nix build --override-input haskell-flake path:${FLAKE}
# Run the devshell test script in a nix develop shell.

View File

@ -24,10 +24,6 @@
];
perSystem = { self', pkgs, ... }: {
haskellProjects.default = {
packages = {
# You can add more than one local package here.
haskell-flake-test.root = ./.; # Assumes ./haskell-flake-test.cabal
};
overrides = self: super: {
# Custom library overrides (here, "foo" comes from a flake input)
foo = self.callCabal2nix "foo" (inputs.haskell-multi-nix + /foo) { };