From 796445bdc26924e21b97f79b4cdfca887c61823f Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Wed, 17 Jul 2024 03:40:24 +0200 Subject: [PATCH] Attrs: Always expand --- src/Nixfmt/Pretty.hs | 2 +- test/correct/float-below-one.nix | 4 +- test/correct/quotes-in-inherit.nix | 9 ++- test/diff/apply/out.nix | 92 ++++++++++++++++++++++-------- test/diff/attr_set/out.nix | 16 ++++-- test/diff/idioms_lib_3/out.nix | 25 ++++++-- test/diff/idioms_lib_4/out.nix | 11 +++- test/diff/idioms_lib_5/out.nix | 30 +++++++--- test/diff/idioms_nixos_2/out.nix | 9 ++- test/diff/idioms_pkgs_1/out.nix | 4 +- test/diff/idioms_pkgs_2/out.nix | 8 ++- test/diff/idioms_pkgs_4/out.nix | 14 +++-- test/diff/idioms_pkgs_5/out.nix | 26 +++++++-- test/diff/if_else/out.nix | 11 +++- test/diff/inherit/out.nix | 8 ++- test/diff/inherit_from/out.nix | 4 +- test/diff/key_value/out.nix | 8 ++- test/diff/with/out.nix | 21 +++++-- 18 files changed, 230 insertions(+), 72 deletions(-) diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index d9e79cf8..84e7de53 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -198,7 +198,7 @@ prettyTerm (List (Ann pre paropen post) items parclose) = pretty (Ann pre paropen Nothing) <> surroundWith line (nest $ pretty post <> prettyItems items) <> pretty parclose -prettyTerm (Set krec paropen items parclose) = prettySet False (krec, paropen, items, parclose) +prettyTerm (Set krec paropen items parclose) = prettySet True (krec, paropen, items, parclose) -- Parentheses prettyTerm (Parenthesized paropen expr (Ann closePre parclose closePost)) = group $ diff --git a/test/correct/float-below-one.nix b/test/correct/float-below-one.nix index e5fdbde0..4ca733e6 100644 --- a/test/correct/float-below-one.nix +++ b/test/correct/float-below-one.nix @@ -1 +1,3 @@ -{ foo = 0.3; } +{ + foo = 0.3; +} diff --git a/test/correct/quotes-in-inherit.nix b/test/correct/quotes-in-inherit.nix index a36f90d9..4d8f018c 100644 --- a/test/correct/quotes-in-inherit.nix +++ b/test/correct/quotes-in-inherit.nix @@ -1 +1,8 @@ -{ inherit ({ "in" = 1; }) "in"; } +{ + inherit + ({ + "in" = 1; + }) + "in" + ; +} diff --git a/test/diff/apply/out.nix b/test/diff/apply/out.nix index 42497b7f..e53a8d06 100644 --- a/test/diff/apply/out.nix +++ b/test/diff/apply/out.nix @@ -106,46 +106,90 @@ } '' { - name1 = function arg { asdf = 1; }; + name1 = function arg { + asdf = 1; + }; - name2 = function arg { asdf = 1; } argument; + name2 = function arg { + asdf = 1; + } argument; - name3 = function arg { asdf = 1; } { qwer = 12345; } argument; + name3 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + } + argument; } { - name1 = function arg { asdf = 1; }; + name1 = function arg { + asdf = 1; + }; name2 = function arg { asdf = 1; # multiline } argument; - name3 = function arg { - asdf = 1; - # multiline - } { qwer = 12345; } argument; + name3 = + function arg + { + asdf = 1; + # multiline + } + { + qwer = 12345; + } + argument; } { - name4 = function arg { asdf = 1; } { - qwer = 12345; - qwer2 = 54321; - } argument; + name4 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + qwer2 = 54321; + } + argument; } { - option1 = function arg { asdf = 1; } { - qwer = 12345; - qwer2 = 54321; - } lastArg; + option1 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + qwer2 = 54321; + } + lastArg; - option2 = function arg { asdf = 1; } { - qwer = 12345; - qwer2 = 54321; - } lastArg; + option2 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + qwer2 = 54321; + } + lastArg; - option3 = function arg { asdf = 1; } { - qwer = 12345; - qwer2 = 54321; - } lastArg; + option3 = + function arg + { + asdf = 1; + } + { + qwer = 12345; + qwer2 = 54321; + } + lastArg; } # https://github.com/kamadorueda/alejandra/issues/372#issuecomment-1435083516 { diff --git a/test/diff/attr_set/out.nix b/test/diff/attr_set/out.nix index 7e5ec04b..bcbe8bd7 100644 --- a/test/diff/attr_set/out.nix +++ b/test/diff/attr_set/out.nix @@ -3,8 +3,12 @@ { # a } - { a = 1; } - { a = 1; } + { + a = 1; + } + { + a = 1; + } { @@ -16,7 +20,9 @@ }; } - { b = 1; } + { + b = 1; + } { b = 1; # c } @@ -29,7 +35,9 @@ b = 1; # c } - rec { c = 1; } + rec { + c = 1; + } rec { c = 1; # d } diff --git a/test/diff/idioms_lib_3/out.nix b/test/diff/idioms_lib_3/out.nix index 1115ae8b..603278a7 100644 --- a/test/diff/idioms_lib_3/out.nix +++ b/test/diff/idioms_lib_3/out.nix @@ -153,7 +153,9 @@ rec { '' [${mkSectionName sectName}] '' - + toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } sectValues; + + toKeyValue { + inherit mkKeyValue listsAsDuplicateKeys; + } sectValues; in # map input to ini sections mapAttrsToStringsSep "\n" mkSection attrsOfAttrs; @@ -208,9 +210,14 @@ rec { if globalSection == { } then "" else - (toKeyValue { inherit mkKeyValue listsAsDuplicateKeys; } globalSection) + "\n" + (toKeyValue { + inherit mkKeyValue listsAsDuplicateKeys; + } globalSection) + + "\n" ) - + (toINI { inherit mkSectionName mkKeyValue listsAsDuplicateKeys; } sections); + + (toINI { + inherit mkSectionName mkKeyValue listsAsDuplicateKeys; + } sections); # Generate a git-config file from an attrset. # @@ -263,13 +270,19 @@ rec { if isAttrs value && !lib.isDerivation value then lib.mapAttrsToList (name: value: recurse ([ name ] ++ path) value) value else if length path > 1 then - { ${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value; } + { + ${concatStringsSep "." (lib.reverseList (tail path))}.${head path} = value; + } else - { ${head path} = value; }; + { + ${head path} = value; + }; in attrs: lib.foldl lib.recursiveUpdate { } (lib.flatten (recurse [ ] attrs)); - toINI_ = toINI { inherit mkKeyValue mkSectionName; }; + toINI_ = toINI { + inherit mkKeyValue mkSectionName; + }; in toINI_ (gitFlattenAttrs attrs); diff --git a/test/diff/idioms_lib_4/out.nix b/test/diff/idioms_lib_4/out.nix index 8ff78dae..36201958 100644 --- a/test/diff/idioms_lib_4/out.nix +++ b/test/diff/idioms_lib_4/out.nix @@ -19,7 +19,9 @@ with lib.lists; with lib.types; with lib.attrsets; with lib.strings; -with (import ./inspect.nix { inherit lib; }).predicates; +with (import ./inspect.nix { + inherit lib; +}).predicates; let inherit (lib.options) mergeOneOption; @@ -29,7 +31,12 @@ let mapAttrs ( name: value: assert type.check value; - setType type.name ({ inherit name; } // value) + setType type.name ( + { + inherit name; + } + // value + ) ); in diff --git a/test/diff/idioms_lib_5/out.nix b/test/diff/idioms_lib_5/out.nix index ee898df8..506991cf 100644 --- a/test/diff/idioms_lib_5/out.nix +++ b/test/diff/idioms_lib_5/out.nix @@ -382,7 +382,9 @@ let null else "key 'meta.${k}' has invalid value; expected ${metaTypes.${k}.description}, got\n ${ - lib.generators.toPretty { indent = " "; } v + lib.generators.toPretty { + indent = " "; + } v }" else "key 'meta.${k}' is unrecognized; expected one of: \n [${ @@ -509,7 +511,9 @@ let } # ----- else - { valid = "yes"; } + { + valid = "yes"; + } ); # The meta attribute is passed in the resulting attribute set, @@ -595,12 +599,22 @@ let # or, alternatively, just output a warning message. handled = { - no = handleEvalIssue { inherit meta attrs; } { - inherit (validity) reason errormsg; - }; - warn = handleEvalWarning { inherit meta attrs; } { - inherit (validity) reason errormsg; - }; + no = + handleEvalIssue + { + inherit meta attrs; + } + { + inherit (validity) reason errormsg; + }; + warn = + handleEvalWarning + { + inherit meta attrs; + } + { + inherit (validity) reason errormsg; + }; yes = true; } .${validity.valid}; diff --git a/test/diff/idioms_nixos_2/out.nix b/test/diff/idioms_nixos_2/out.nix index e7d3e5d2..53a85026 100644 --- a/test/diff/idioms_nixos_2/out.nix +++ b/test/diff/idioms_nixos_2/out.nix @@ -51,7 +51,10 @@ let post_max_size = cfg.maxUploadSize; memory_limit = cfg.maxUploadSize; } - // cfg.phpOptions // optionalAttrs cfg.caching.apcu { "apc.enable_cli" = "1"; }; + // cfg.phpOptions + // optionalAttrs cfg.caching.apcu { + "apc.enable_cli" = "1"; + }; occ = pkgs.writeScriptBin "nextcloud-occ" '' #! ${pkgs.runtimeShell} @@ -1051,7 +1054,9 @@ in ln -sfT \ ${ pkgs.linkFarm "nix-apps" ( - mapAttrsToList (name: path: { inherit name path; }) cfg.extraApps + mapAttrsToList (name: path: { + inherit name path; + }) cfg.extraApps ) } \ ${cfg.home}/nix-apps diff --git a/test/diff/idioms_pkgs_1/out.nix b/test/diff/idioms_pkgs_1/out.nix index afdbed26..12c0e5c3 100644 --- a/test/diff/idioms_pkgs_1/out.nix +++ b/test/diff/idioms_pkgs_1/out.nix @@ -8,7 +8,9 @@ stdenv.mkDerivation rec { pname = "test"; version = "0.0"; - src = fetchFrom { url = "example/${version}"; }; + src = fetchFrom { + url = "example/${version}"; + }; meta = with lib; { maintainers = with maintainers; [ someone ]; description = "something"; diff --git a/test/diff/idioms_pkgs_2/out.nix b/test/diff/idioms_pkgs_2/out.nix index 84f50d54..69619cc8 100644 --- a/test/diff/idioms_pkgs_2/out.nix +++ b/test/diff/idioms_pkgs_2/out.nix @@ -20,12 +20,16 @@ stdenv.mkDerivation rec { doCheck = true; passthru.tests = { - version = testVersion { package = hello; }; + version = testVersion { + package = hello; + }; invariant-under-noXlibs = testEqualDerivation "hello must not be rebuilt when environment.noXlibs is set." hello - (nixos { environment.noXlibs = true; }).pkgs.hello; + (nixos { + environment.noXlibs = true; + }).pkgs.hello; }; meta = with lib; { diff --git a/test/diff/idioms_pkgs_4/out.nix b/test/diff/idioms_pkgs_4/out.nix index a4ed06e4..0372247f 100644 --- a/test/diff/idioms_pkgs_4/out.nix +++ b/test/diff/idioms_pkgs_4/out.nix @@ -197,9 +197,13 @@ in # First build a stdenv based only on tools outside the store. (prevStage: { inherit config overlays; - stdenv = makeStdenv { inherit (prevStage) cc fetchurl; } // { - inherit (prevStage) fetchurl; - }; + stdenv = + makeStdenv { + inherit (prevStage) cc fetchurl; + } + // { + inherit (prevStage) fetchurl; + }; }) # Using that, build a stdenv that adds the ‘xz’ command (which most systems @@ -209,7 +213,9 @@ in stdenv = makeStdenv { inherit (prevStage.stdenv) cc fetchurl; extraPath = [ prevStage.xz ]; - overrides = self: super: { inherit (prevStage) xz; }; + overrides = self: super: { + inherit (prevStage) xz; + }; extraNativeBuildInputs = if localSystem.isLinux then [ prevStage.patchelf ] else [ ]; }; diff --git a/test/diff/idioms_pkgs_5/out.nix b/test/diff/idioms_pkgs_5/out.nix index fc02b21a..92448cf2 100644 --- a/test/diff/idioms_pkgs_5/out.nix +++ b/test/diff/idioms_pkgs_5/out.nix @@ -56,7 +56,12 @@ let # separate lines, because Nix would only show the last line of the comment. # An infinite recursion here can be caused by having the attribute names of expression `e` in `.overrideAttrs(finalAttrs: previousAttrs: e)` depend on `finalAttrs`. Only the attribute values of `e` can depend on `finalAttrs`. - args = rattrs (args // { inherit finalPackage overrideAttrs; }); + args = rattrs ( + args + // { + inherit finalPackage overrideAttrs; + } + ); # ^^^^ overrideAttrs = @@ -500,7 +505,9 @@ let "${attrs.pname}${staticMarker}${hostSuffix}-${attrs.version}" ); }) - // optionalAttrs __structuredAttrs { env = checkedEnv; } + // optionalAttrs __structuredAttrs { + env = checkedEnv; + } // { builder = attrs.realBuilder or stdenv.shell; args = @@ -628,9 +635,14 @@ let enableParallelChecking = attrs.enableParallelChecking or true; enableParallelInstalling = attrs.enableParallelInstalling or true; } - // optionalAttrs ( - hardeningDisable != [ ] || hardeningEnable != [ ] || stdenv.hostPlatform.isMusl - ) { NIX_HARDENING_ENABLE = enabledHardeningOptions; } + // + optionalAttrs + ( + hardeningDisable != [ ] || hardeningEnable != [ ] || stdenv.hostPlatform.isMusl + ) + { + NIX_HARDENING_ENABLE = enabledHardeningOptions; + } // optionalAttrs (stdenv.hostPlatform.isx86_64 && stdenv.hostPlatform ? gcc.arch) { @@ -712,7 +724,9 @@ let references ; }; - validity = checkMeta.assertValidity { inherit meta attrs; }; + validity = checkMeta.assertValidity { + inherit meta attrs; + }; checkedEnv = let diff --git a/test/diff/if_else/out.nix b/test/diff/if_else/out.nix index 479cd610..15848854 100644 --- a/test/diff/if_else/out.nix +++ b/test/diff/if_else/out.nix @@ -1,5 +1,14 @@ [ - (if true then { version = "1.2.3"; } else { version = "3.2.1"; }) + ( + if true then + { + version = "1.2.3"; + } + else + { + version = "3.2.1"; + } + ) ( if true then '' diff --git a/test/diff/inherit/out.nix b/test/diff/inherit/out.nix index 93a31869..30594acd 100644 --- a/test/diff/inherit/out.nix +++ b/test/diff/inherit/out.nix @@ -18,8 +18,12 @@ j ; } - { inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; } - { inherit b d; } + { + inherit aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; + } + { + inherit b d; + } { inherit b diff --git a/test/diff/inherit_from/out.nix b/test/diff/inherit_from/out.nix index 126b8f8b..aaaf6847 100644 --- a/test/diff/inherit_from/out.nix +++ b/test/diff/inherit_from/out.nix @@ -40,7 +40,9 @@ k ; } - { inherit (c) f h; } + { + inherit (c) f h; + } { inherit (c) f diff --git a/test/diff/key_value/out.nix b/test/diff/key_value/out.nix index 1d93de5b..0470f894 100644 --- a/test/diff/key_value/out.nix +++ b/test/diff/key_value/out.nix @@ -41,14 +41,18 @@ rec { 1 # d ; }; - j = a: { b = 1; }; + j = a: { + b = 1; + }; k = a: { b = 1; c = 2; }; l = a: # b - { b = 1; }; + { + b = 1; + }; m = a: # b { diff --git a/test/diff/with/out.nix b/test/diff/with/out.nix index 9c71920b..bcb43cef 100644 --- a/test/diff/with/out.nix +++ b/test/diff/with/out.nix @@ -16,8 +16,12 @@ ) (with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) (with b; cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc) - { a = with b; 1; } - { a = with b; 1 + 1; } + { + a = with b; 1; + } + { + a = with b; 1 + 1; + } { a = with b; { c = 1; @@ -62,7 +66,14 @@ 3 ]) (with a; with b; with c; [ 1 ]) - (with a; with b; with c; { a = 1; }) + ( + with a; + with b; + with c; + { + a = 1; + } + ) ( with a; # comment with b; @@ -89,7 +100,9 @@ b = 2; } ) - { a = with b; with b; with b; 1; } + { + a = with b; with b; with b; 1; + } { binPath = with pkgs;