From b73fff3e7cdeaf167f40478ddbfb5b5edf2e8e5c Mon Sep 17 00:00:00 2001 From: Rashid Al Muhairi Date: Mon, 6 Feb 2023 17:17:40 +0400 Subject: [PATCH 1/5] ocamlformat: Modify package option to support version detection --- programs/ocamlformat.nix | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/programs/ocamlformat.nix b/programs/ocamlformat.nix index f27de6e..0310736 100644 --- a/programs/ocamlformat.nix +++ b/programs/ocamlformat.nix @@ -4,17 +4,38 @@ , ... }: let + inherit (lib) types; + cfg = config.programs.ocamlformat; + + detectVersion = configFile: + let + optionValue = list: + assert lib.assertMsg (list != [ ]) "treefmt/ocamlformat: Unable to find version in the config file"; + lib.elemAt list (lib.length list - 1); + in + builtins.getAttr "ocamlformat_${ + builtins.replaceStrings ["."] ["_"] + (optionValue (lib.findFirst (option: builtins.head option == "version") [] + (builtins.map (n: lib.splitString "=" n) (lib.splitString "\n" (builtins.readFile configFile))))) + }" + pkgs; in { options.programs.ocamlformat = { enable = lib.mkEnableOption "ocamlformat"; - package = lib.mkPackageOption pkgs "ocamlformat" { }; + package = lib.mkOption { + type = types.either types.path types.package; + default = pkgs.ocamlformat; + }; }; config = lib.mkIf cfg.enable { settings.formatter.ocamlformat = { - command = cfg.package; + command = + if lib.isPath cfg.package + then detectVersion cfg.package + else cfg.package; options = [ "-i" ]; includes = [ "*.ml" "*.mli" ]; }; From 82f2a0301b610eb69bfc5920d0bab29361ea1401 Mon Sep 17 00:00:00 2001 From: Rashid Al Muhairi Date: Mon, 6 Feb 2023 20:17:25 +0400 Subject: [PATCH 2/5] ocamlformat: Allow for custom package sets when specifying path --- programs/ocamlformat.nix | 51 ++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/programs/ocamlformat.nix b/programs/ocamlformat.nix index 0310736..f80ed93 100644 --- a/programs/ocamlformat.nix +++ b/programs/ocamlformat.nix @@ -1,31 +1,42 @@ -{ lib -, pkgs -, config -, ... -}: -let +{ + lib, + pkgs, + config, + ... +}: let inherit (lib) types; cfg = config.programs.ocamlformat; - detectVersion = configFile: - let - optionValue = list: - assert lib.assertMsg (list != [ ]) "treefmt/ocamlformat: Unable to find version in the config file"; + detectVersion = configFile: pkgSet: let + optionValue = list: + assert lib.assertMsg (list != []) "treefmt/ocamlformat: Unable to find version in the config file"; lib.elemAt list (lib.length list - 1); - in + in builtins.getAttr "ocamlformat_${ builtins.replaceStrings ["."] ["_"] (optionValue (lib.findFirst (option: builtins.head option == "version") [] (builtins.map (n: lib.splitString "=" n) (lib.splitString "\n" (builtins.readFile configFile))))) }" - pkgs; -in -{ + pkgSet; +in { options.programs.ocamlformat = { enable = lib.mkEnableOption "ocamlformat"; package = lib.mkOption { - type = types.either types.path types.package; + type = types.oneOf [ + types.path + types.package + + (types.submodule { + options = { + pkgs = lib.mkOption { + type = types.lazyAttrsOf types.raw; + default = pkgs; + }; + path = lib.mkOption {type = types.path;}; + }; + }) + ]; default = pkgs.ocamlformat; }; }; @@ -33,11 +44,11 @@ in config = lib.mkIf cfg.enable { settings.formatter.ocamlformat = { command = - if lib.isPath cfg.package - then detectVersion cfg.package - else cfg.package; - options = [ "-i" ]; - includes = [ "*.ml" "*.mli" ]; + if lib.isDerivation cfg.package + then cfg.package + else detectVersion cfg.package.path cfg.package.pkgs; + options = ["-i"]; + includes = ["*.ml" "*.mli"]; }; }; } From a763fb087f4b91623a87c70bf75d030238619477 Mon Sep 17 00:00:00 2001 From: Rashid Al Muhairi Date: Tue, 7 Feb 2023 03:38:43 +0400 Subject: [PATCH 3/5] ocamlformat: Format the module and modify the error message for clarity --- programs/ocamlformat.nix | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/programs/ocamlformat.nix b/programs/ocamlformat.nix index f80ed93..7063cc1 100644 --- a/programs/ocamlformat.nix +++ b/programs/ocamlformat.nix @@ -1,39 +1,40 @@ -{ - lib, - pkgs, - config, - ... -}: let +{ lib +, pkgs +, config +, ... +}: +let inherit (lib) types; cfg = config.programs.ocamlformat; - detectVersion = configFile: pkgSet: let - optionValue = list: - assert lib.assertMsg (list != []) "treefmt/ocamlformat: Unable to find version in the config file"; + detectVersion = configFile: pkgSet: + let + optionValue = list: + assert lib.assertMsg (list != [ ]) "treefmt-nix: Unable to detect ocamlformat version from file"; lib.elemAt list (lib.length list - 1); - in + in builtins.getAttr "ocamlformat_${ builtins.replaceStrings ["."] ["_"] (optionValue (lib.findFirst (option: builtins.head option == "version") [] (builtins.map (n: lib.splitString "=" n) (lib.splitString "\n" (builtins.readFile configFile))))) }" - pkgSet; -in { + pkgSet; +in +{ options.programs.ocamlformat = { enable = lib.mkEnableOption "ocamlformat"; package = lib.mkOption { type = types.oneOf [ types.path types.package - (types.submodule { options = { pkgs = lib.mkOption { type = types.lazyAttrsOf types.raw; default = pkgs; }; - path = lib.mkOption {type = types.path;}; + path = lib.mkOption { type = types.path; }; }; }) ]; @@ -47,8 +48,8 @@ in { if lib.isDerivation cfg.package then cfg.package else detectVersion cfg.package.path cfg.package.pkgs; - options = ["-i"]; - includes = ["*.ml" "*.mli"]; + options = [ "-i" ]; + includes = [ "*.ml" "*.mli" ]; }; }; } From d6b2e5517565ca6e25fb23d8455d69b5283dd10e Mon Sep 17 00:00:00 2001 From: Rashid Al Muhairi Date: Tue, 7 Feb 2023 04:25:27 +0400 Subject: [PATCH 4/5] ocamlformat: fix case where only a file is passed --- programs/ocamlformat.nix | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/programs/ocamlformat.nix b/programs/ocamlformat.nix index 7063cc1..9e99622 100644 --- a/programs/ocamlformat.nix +++ b/programs/ocamlformat.nix @@ -30,11 +30,10 @@ in types.package (types.submodule { options = { + path = lib.mkOption { type = types.path; }; pkgs = lib.mkOption { type = types.lazyAttrsOf types.raw; - default = pkgs; }; - path = lib.mkOption { type = types.path; }; }; }) ]; @@ -47,7 +46,7 @@ in command = if lib.isDerivation cfg.package then cfg.package - else detectVersion cfg.package.path cfg.package.pkgs; + else detectVersion (cfg.package.path or cfg.package) (cfg.package.pkgs or pkgs); options = [ "-i" ]; includes = [ "*.ml" "*.mli" ]; }; From 45d73a8977fd17777b04b45ed42cc8f73a1765aa Mon Sep 17 00:00:00 2001 From: Rashid Al Muhairi Date: Wed, 8 Feb 2023 23:00:04 +0400 Subject: [PATCH 5/5] ocamlformat: Separate version detection from the package option onto two other options, pkgs and configFile --- programs/ocamlformat.nix | 49 +++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/programs/ocamlformat.nix b/programs/ocamlformat.nix index 9e99622..394a277 100644 --- a/programs/ocamlformat.nix +++ b/programs/ocamlformat.nix @@ -4,49 +4,46 @@ , ... }: let - inherit (lib) types; - + l = lib // builtins; cfg = config.programs.ocamlformat; detectVersion = configFile: pkgSet: let optionValue = list: - assert lib.assertMsg (list != [ ]) "treefmt-nix: Unable to detect ocamlformat version from file"; - lib.elemAt list (lib.length list - 1); + assert l.assertMsg (list != [ ]) "treefmt-nix: Unable to detect ocamlformat version from file"; + l.elemAt list (l.length list - 1); in - builtins.getAttr "ocamlformat_${ - builtins.replaceStrings ["."] ["_"] - (optionValue (lib.findFirst (option: builtins.head option == "version") [] - (builtins.map (n: lib.splitString "=" n) (lib.splitString "\n" (builtins.readFile configFile))))) + l.getAttr "ocamlformat_${ + l.replaceStrings ["."] ["_"] + (optionValue (l.findFirst (option: l.head option == "version") [] + (l.map (n: l.splitString "=" n) (l.splitString "\n" (l.readFile configFile))))) }" pkgSet; in { options.programs.ocamlformat = { - enable = lib.mkEnableOption "ocamlformat"; - package = lib.mkOption { - type = types.oneOf [ - types.path - types.package - (types.submodule { - options = { - path = lib.mkOption { type = types.path; }; - pkgs = lib.mkOption { - type = types.lazyAttrsOf types.raw; - }; - }; - }) - ]; - default = pkgs.ocamlformat; + enable = l.mkEnableOption "ocamlformat"; + package = l.mkPackageOption pkgs "ocamlformat" { }; + + pkgs = l.mkOption { + type = l.types.lazyAttrsOf l.types.raw; + description = "The package set used to get the ocamlformat package at a specific version. defaults to nixpkgs."; + default = pkgs; + }; + + configFile = l.mkOption { + type = l.types.nullOr l.types.path; + description = "Path to the .ocamlformat file. Used to pick the right version of ocamlformat if passed."; + default = null; }; }; - config = lib.mkIf cfg.enable { + config = l.mkIf cfg.enable { settings.formatter.ocamlformat = { command = - if lib.isDerivation cfg.package + if l.isNull cfg.configFile then cfg.package - else detectVersion (cfg.package.path or cfg.package) (cfg.package.pkgs or pkgs); + else detectVersion cfg.configFile cfg.pkgs; options = [ "-i" ]; includes = [ "*.ml" "*.mli" ]; };