feat: Add granular settings defaults (#275)

This commit is contained in:
Shivaraj B H 2024-04-20 13:14:10 +05:30 committed by GitHub
parent 847292fc79
commit 53e434d194
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 264 additions and 18 deletions

View File

@ -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)

View File

@ -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 = {
@ -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 = {

View File

@ -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 = ''
@ -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;

View File

@ -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.
@ -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 {

View File

@ -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

View File

@ -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";
};
};
};
};
}

View File

@ -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

View File

@ -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