Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add granular settings defaults (defaults.settings.{local, defined, all}) #275

Merged
merged 14 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `settings` module:
- #210: Add `extraLibraries` to `settings` module.
- #225: Add `removeReferencesTo` to `settings` module.
- #275: Fine grained settings defaults via `defaults.settings.{local, defined, all}`
- #277: Add `otherOverlays` option to add custom Haskell package overlays.
- #215: Improved debug logging.
- #216: Remove `debug` option (pass `--trace-verbose` to nix instead)
Expand Down
8 changes: 8 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
flake-parts = "github:hercules-ci/flake-parts/" + exampleLock.nodes.flake-parts.locked.rev;
haskell-flake = ./.;
haskell-parsers = ./nix/haskell-parsers;
haskell-template = "github:srid/haskell-template/554b7c565396cf2d49a248e7e1dc0e0b46883b10";
in
{
dev = {
Expand Down Expand Up @@ -65,6 +66,13 @@
};
};

test-settings-defaults = {
dir = "test/settings-defaults";
overrideInputs = {
inherit nixpkgs flake-parts haskell-flake haskell-template;
};
};

test-otherOverlays = {
dir = "test/otherOverlays";
overrideInputs = {
Expand Down
10 changes: 10 additions & 0 deletions nix/modules/project-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ in
Each patch can be a path to the diff file, or inline patch string.
'';
};
extraHaskellProjectConfig = lib.mkOption {
type = types.deferredModule;
description = ''
Extra configuration to apply to the patched haskell-flake project.
'';
default = { };
};
expect = lib.mkOption {
type = types.raw;
description = ''
Expand All @@ -53,6 +60,9 @@ in

config = {
haskellProjects = lib.flip lib.mapAttrs config.haskellProjectTests (name: cfg: {
imports = [
cfg.extraHaskellProjectConfig
];
projectRoot = pkgs.applyPatches {
name = "haskellProject-patched-${name}";
src = config.haskellProjects.${cfg.from}.projectRoot;
Expand Down
131 changes: 114 additions & 17 deletions nix/modules/project/defaults.nix
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,68 @@ in
'';
};

settings.default = mkOption {
settings.local = mkOption {
type = types.deferredModule;
description = ''
Default settings for all packages in `packages` option.
Default settings for packages local to the current project.
'';
apply = settings:
if config.defaults.enable then
{ package, ... }:
lib.optionalAttrs (package.local.toCurrentProject or false) {
imports = [
settings
];
}
else { };
default = { };
};

settings.defined = mkOption {
type = types.deferredModule;
description = ''
Default settings for all the packages defined using haskell-flake.

For example,
```nix
{
# Inside haskellProjects.<name>
imports = [
inputs.moo.haskellFlakeProjectModules.output
];
packages = {
foo.source = "0.1";
bar.source = inputs.bar;
};
settings = {
baz.check = false;
};
}
```
and
```cabal
...
build-depends:
moo
, foo
, bar
, baz
, qux
...
```
This will apply the settings to `moo` and packages in current project. But not to `foo`, `bar`, `baz` and `qux`.
'';

apply = settings:
if config.defaults.enable then
{ package, ... }:
lib.optionalAttrs (package.local.toDefinedProject or false) {
imports = [
settings
];
}
else { };

defaultText = ''
Speed up builds by disabling haddock and library profiling.

Expand All @@ -82,23 +139,63 @@ in
haskell-flake). The goal being to use the same configuration
consistently for all packages using haskell-flake.
'';
default =
let
localSettings = { name, package, config, ... }:
lib.optionalAttrs (package.local.toDefinedProject or false) {
# Disabling haddock and profiling is mainly to speed up Nix builds.
haddock = lib.mkDefault false; # Because, this is end-user software. No need for library docs.
libraryProfiling = lib.mkDefault false; # Avoid double-compilation.
# Make sure all files we use are included in the sdist, as a check
# for release-worthiness.
buildFromSdist = lib.mkDefault true;
};
in
if config.defaults.enable then {

default = {
# Disabling haddock and profiling is mainly to speed up Nix builds.
haddock = lib.mkDefault false; # Because, this is end-user software. No need for library docs.
libraryProfiling = lib.mkDefault false; # Avoid double-compilation.
};
};

settings.all = mkOption {
type = types.deferredModule;
description = ''
Default settings for all packages whose derivations are produced by haskell-flake.

For example,
```nix
{
# Inside haskellProjects.<name>
imports = [
localSettings
inputs.moo.haskellFlakeProjectModules.output
];
} else { };
packages = {
foo.source = "0.1";
bar.source = inputs.bar;
};
settings = {
baz.check = false;
};
}
```
and
```cabal
...
build-depends:
moo
, foo
, bar
, baz
, qux
...
```
This will apply the settings to `moo`, `foo`, `bar`, `baz`. But not to `qux`.
'';

apply = settings:
if config.defaults.enable then
{
imports = [
settings
];
}
else { };
defaultText = ''
Make sure all files we use are included in the sdist, as a check for release-worthiness.
'';
default = {
buildFromSdist = lib.mkDefault true;
};
};

projectModules.output = mkOption {
Expand Down
5 changes: 4 additions & 1 deletion nix/modules/project/settings/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ in
./all.nix

# Default settings
project.config.defaults.settings.default
project.config.defaults.settings.local
project.config.defaults.settings.defined
project.config.defaults.settings.all


# User module
mod
Expand Down
99 changes: 99 additions & 0 deletions test/settings-defaults/flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
# Since there is no flake.lock file (to avoid incongruent haskell-flake
# pinning), we must specify revisions for *all* inputs to ensure
# reproducibility.
inputs = {
nixpkgs = { };
flake-parts = { };
haskell-flake = { };
haskell-template = { };
};
outputs = inputs@{ self, nixpkgs, flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = nixpkgs.lib.systems.flakeExposed;
imports = [
inputs.haskell-flake.flakeModule
];
debug = true;
perSystem = { config, self', pkgs, lib, ... }: {
haskellProjects.default = { };
haskellProjectTests =
let
finalPackagesOf = projectName: config.haskellProjects.${projectName}.outputs.finalPackages;
isSettingApplied = pkg: lib.hasAttr "setting-applied" pkg.meta;
in
{
test-default-current = { name, ... }: {
patches = [ ];
extraHaskellProjectConfig = {
imports = [
inputs.haskell-template.haskellFlakeProjectModules.output
];
defaults.settings.local = {
custom = pkg: pkg.overrideAttrs (oldAttrs: {
meta = oldAttrs.meta // {
setting-applied = true;
};
});
};
};
expect =
lib.assertMsg
(lib.all (x: x) [
(! isSettingApplied (finalPackagesOf name).random)
(! isSettingApplied (finalPackagesOf name).haskell-template)
(isSettingApplied (finalPackagesOf name).haskell-flake-test)
])
"defaults.settings: ${name} failed";
};
test-default-defined = { name, ... }: {
patches = [ ];
extraHaskellProjectConfig = {
imports = [
inputs.haskell-template.haskellFlakeProjectModules.output
];
defaults.settings.defined = {
custom = pkg: pkg.overrideAttrs (oldAttrs: {
meta = oldAttrs.meta // {
setting-applied = true;
};
});
};
};
expect =
lib.assertMsg
(lib.all (x: x) [
(! isSettingApplied (finalPackagesOf name).random)
(isSettingApplied (finalPackagesOf name).haskell-template)
(isSettingApplied (finalPackagesOf name).haskell-flake-test)
])
"defaults.settings: ${name} failed";
};
test-default-all = { name, ... }: {
patches = [ ];
extraHaskellProjectConfig = {
imports = [
inputs.haskell-template.haskellFlakeProjectModules.output
];
settings.random = { };
defaults.settings.all = {
custom = pkg: pkg.overrideAttrs (oldAttrs: {
meta = oldAttrs.meta // {
setting-applied = true;
};
});
};
};
expect =
lib.assertMsg
(lib.all (x: x) [
(isSettingApplied (finalPackagesOf name).random)
(isSettingApplied (finalPackagesOf name).haskell-template)
(isSettingApplied (finalPackagesOf name).haskell-flake-test)
])
"defaults.settings: ${name} failed";
};
};
};
};
}
19 changes: 19 additions & 0 deletions test/settings-defaults/haskell-flake-test.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cabal-version: 3.0
name: haskell-flake-test
version: 0.1.0.0
license: NONE
author: Joe
maintainer: joe@example.com
build-type: Simple

common warnings
ghc-options: -Wall

executable haskell-flake-test
import: warnings
main-is: Main.hs
build-depends:
base,
random
hs-source-dirs: src
default-language: Haskell2010
9 changes: 9 additions & 0 deletions test/settings-defaults/src/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Main where

import System.Random

main :: IO ()
main = do
-- Generate a random number between 1 and 100 (inclusive)
randomNumber <- randomRIO (1, 100) :: IO Int
putStrLn $ "Hello " ++ show randomNumber
Loading