From 3479354ec92e0c5b73d4ddb4cc79c6d3580f1724 Mon Sep 17 00:00:00 2001 From: Jake Hamilton Date: Sat, 22 Jun 2024 10:56:18 -0700 Subject: [PATCH] refactor: add formatting script --- format.sh | 4 ++ foundation/default.nix | 13 +++-- foundation/flake.nix | 46 ++++++++--------- lib/default.test.nix | 115 +++++++++++++++++++---------------------- lib/flake.nix | 4 +- tidepool/default.nix | 30 +++++------ tidepool/flake.nix | 14 ++--- 7 files changed, 105 insertions(+), 121 deletions(-) create mode 100755 format.sh diff --git a/format.sh b/format.sh new file mode 100755 index 0000000..1ebd942 --- /dev/null +++ b/format.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -I "nixpkgs=https://github.com/nixos/nixpkgs/archive/nixos-24.05.tar.gz" -p nixfmt-rfc-style + +nixfmt ${1:-"--verify"} ./**/*.nix diff --git a/foundation/default.nix b/foundation/default.nix index 3b7fd95..e80393f 100644 --- a/foundation/default.nix +++ b/foundation/default.nix @@ -1,14 +1,13 @@ -{system ? builtins.currentSystem}: let +{ + system ? builtins.currentSystem, +}: +let lib = import ./../lib; modules = import ./src; result = lib.modules.run { - modules = - (builtins.attrValues modules) - ++ [ - {config.aux.system = system;} - ]; + modules = (builtins.attrValues modules) ++ [ { config.aux.system = system; } ]; }; in - result.config.exports.resolved.packages +result.config.exports.resolved.packages diff --git a/foundation/flake.nix b/foundation/flake.nix index d2bcde4..abe9dc7 100644 --- a/foundation/flake.nix +++ b/foundation/flake.nix @@ -7,34 +7,30 @@ }; }; - outputs = inputs: let - inherit (inputs.lib) lib; + outputs = + inputs: + let + inherit (inputs.lib) lib; - modules = import ./src; + modules = import ./src; - forEachSystem = lib.attrs.generate [ - "i686-linux" - ]; - in { - extras = let - result = lib.modules.run { - modules = - builtins.attrValues modules; - }; + forEachSystem = lib.attrs.generate [ "i686-linux" ]; in - result.config.exports.resolved.extras; + { + extras = + let + result = lib.modules.run { modules = builtins.attrValues modules; }; + in + result.config.exports.resolved.extras; - packages = forEachSystem ( - system: let - result = lib.modules.run { - modules = - (builtins.attrValues modules) - ++ [ - {config.aux.system = system;} - ]; - }; - in + packages = forEachSystem ( + system: + let + result = lib.modules.run { + modules = (builtins.attrValues modules) ++ [ { config.aux.system = system; } ]; + }; + in result.config.exports.resolved.packages - ); - }; + ); + }; } diff --git a/lib/default.test.nix b/lib/default.test.nix index 8f243b3..0decee6 100644 --- a/lib/default.test.nix +++ b/lib/default.test.nix @@ -25,54 +25,51 @@ let ./src/versions/default.test.nix ]; - resolve = file: let - imported = import file; - value = - if builtins.isFunction imported - then imported {inherit lib;} - else imported; - relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file); - in { - inherit file value; - relative = - if lib.strings.hasPrefix "/" relative - then "." + relative - else relative; - namespace = getNamespace file; - }; + resolve = + file: + let + imported = import file; + value = if builtins.isFunction imported then imported { inherit lib; } else imported; + relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file); + in + { + inherit file value; + relative = if lib.strings.hasPrefix "/" relative then "." + relative else relative; + namespace = getNamespace file; + }; resolved = builtins.map resolve files; - getNamespace = path: let - relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString path); - parts = lib.strings.split "/" relative; - in - if builtins.length parts > 2 - then builtins.elemAt parts 2 - else relative; - - results = let - getTests = file: prefix: suite: let - nested = lib.attrs.mapToList (name: value: getTests file (prefix ++ [name]) value) suite; - relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file); + getNamespace = + path: + let + relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString path); + parts = lib.strings.split "/" relative; in - if builtins.isAttrs suite - then builtins.concatLists nested - else [ - { - inherit prefix file; - name = builtins.concatStringsSep " > " prefix; - value = suite; - relative = - if lib.strings.hasPrefix "/" relative - then "." + relative - else relative; - } - ]; + if builtins.length parts > 2 then builtins.elemAt parts 2 else relative; - base = - builtins.map (entry: getTests entry.file [entry.namespace] entry.value) resolved; - in + results = + let + getTests = + file: prefix: suite: + let + nested = lib.attrs.mapToList (name: value: getTests file (prefix ++ [ name ]) value) suite; + relative = lib.strings.removePrefix (builtins.toString root) (builtins.toString file); + in + if builtins.isAttrs suite then + builtins.concatLists nested + else + [ + { + inherit prefix file; + name = builtins.concatStringsSep " > " prefix; + value = suite; + relative = if lib.strings.hasPrefix "/" relative then "." + relative else relative; + } + ]; + + base = builtins.map (entry: getTests entry.file [ entry.namespace ] entry.value) resolved; + in builtins.concatLists base; successes = builtins.filter (test: test.value) results; @@ -80,29 +77,23 @@ let total = "${builtins.toString (builtins.length successes)} / ${builtins.toString (builtins.length results)}"; in - if failures == [] - then let - message = - lib.strings.concatMapSep "\n" - (test: "✅ ${test.name}") - successes; - in '' +if failures == [ ] then + let + message = lib.strings.concatMapSep "\n" (test: "✅ ${test.name}") successes; + in + '' SUCCESS (${total}) ${message} '' - else let - successMessage = - lib.strings.concatMapSep "\n" - (test: "✅ ${test.name}") - successes; - failureMessage = - lib.strings.concatMapSep "\n\n" - (test: - "❎ ${test.name}\n" - + " -> ${test.relative}") - failures; - in '' +else + let + successMessage = lib.strings.concatMapSep "\n" (test: "✅ ${test.name}") successes; + failureMessage = lib.strings.concatMapSep "\n\n" ( + test: "❎ ${test.name}\n" + " -> ${test.relative}" + ) failures; + in + '' FAILURE (${total}) ${failureMessage} diff --git a/lib/flake.nix b/lib/flake.nix index bf835a3..749d2de 100644 --- a/lib/flake.nix +++ b/lib/flake.nix @@ -1,7 +1,5 @@ { description = "A NixPkgs library replacement containing helper functions and a module system."; - outputs = _: { - lib = import ./src; - }; + outputs = _: { lib = import ./src; }; } diff --git a/tidepool/default.nix b/tidepool/default.nix index 6b039d1..8c6436e 100644 --- a/tidepool/default.nix +++ b/tidepool/default.nix @@ -1,27 +1,21 @@ { lib ? import ./../lib, - foundation ? import ./../foundation {system = "i686-linux";}, -}: let + foundation ? import ./../foundation { system = "i686-linux"; }, +}: +let modules = import ./src/modules.nix; result = lib.modules.run { - modules = - (builtins.attrValues modules) - ++ [ - ./src/export.nix - { - __file__ = ./default.nix; + modules = (builtins.attrValues modules) ++ [ + ./src/export.nix + { + __file__ = ./default.nix; - options.foundation = lib.options.create { - type = lib.types.attrs.of lib.types.derivation; - }; + options.foundation = lib.options.create { type = lib.types.attrs.of lib.types.derivation; }; - config.foundation = foundation; - } - ]; + config.foundation = foundation; + } + ]; }; in - result.config.exported - // { - inherit (result) config; - } +result.config.exported // { inherit (result) config; } diff --git a/tidepool/flake.nix b/tidepool/flake.nix index e98c783..213cd18 100644 --- a/tidepool/flake.nix +++ b/tidepool/flake.nix @@ -9,11 +9,13 @@ }; }; - outputs = inputs: let - exports = import ./default.nix { - lib = inputs.lib.lib; - foundation = inputs.foundation.packages.i686-linux; - }; - in + outputs = + inputs: + let + exports = import ./default.nix { + lib = inputs.lib.lib; + foundation = inputs.foundation.packages.i686-linux; + }; + in exports; }