From 59870d08783118e234452f79049188f799a66093 Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Wed, 16 Nov 2022 17:06:53 +0300 Subject: [PATCH] refactor: separate utils into own modules --- src/default.nix | 12 ++-- src/lib/default.nix | 9 +++ .../functions.translators/implementation.nix | 2 +- src/modules/top-level.nix | 5 ++ src/modules/utils.dream-lock/default.nix | 6 ++ .../implementation.nix} | 4 +- src/modules/utils.dream-lock/interface.nix | 35 ++++++++++++ .../index => utils.index}/build-script.py | 0 src/modules/utils.index/default.nix | 6 ++ .../implementation.nix} | 0 src/modules/utils.index/interface.nix | 14 +++++ .../index => utils.index}/make-stats.py | 0 src/modules/utils.override/default.nix | 6 ++ .../implementation.nix} | 0 src/modules/utils.override/interface.nix | 14 +++++ src/modules/utils.toTOML/default.nix | 6 ++ .../implementation.nix} | 0 src/modules/utils.toTOML/interface.nix | 10 ++++ src/modules/utils.translator/default.nix | 6 ++ .../implementation.nix} | 0 src/modules/utils.translator/interface.nix | 10 ++++ src/modules/utils/implementation.nix | 7 --- src/modules/utils/interface.nix | 56 +------------------ .../go/translators/gomod2nix/translate.nix | 2 +- src/updaters/default.nix | 6 +- tests/unit/test_dream_lock.py | 4 +- 26 files changed, 143 insertions(+), 77 deletions(-) create mode 100644 src/modules/utils.dream-lock/default.nix rename src/modules/{utils/dream-lock.nix => utils.dream-lock/implementation.nix} (98%) create mode 100644 src/modules/utils.dream-lock/interface.nix rename src/modules/{utils/index => utils.index}/build-script.py (100%) create mode 100644 src/modules/utils.index/default.nix rename src/modules/{utils/index/default.nix => utils.index/implementation.nix} (100%) create mode 100644 src/modules/utils.index/interface.nix rename src/modules/{utils/index => utils.index}/make-stats.py (100%) create mode 100644 src/modules/utils.override/default.nix rename src/modules/{utils/override.nix => utils.override/implementation.nix} (100%) create mode 100644 src/modules/utils.override/interface.nix create mode 100644 src/modules/utils.toTOML/default.nix rename src/modules/{utils/toTOML.nix => utils.toTOML/implementation.nix} (100%) create mode 100644 src/modules/utils.toTOML/interface.nix create mode 100644 src/modules/utils.translator/default.nix rename src/modules/{utils/translator.nix => utils.translator/implementation.nix} (100%) create mode 100644 src/modules/utils.translator/interface.nix diff --git a/src/default.nix b/src/default.nix index 414e0600..5618cf75 100644 --- a/src/default.nix +++ b/src/default.nix @@ -263,7 +263,7 @@ in let sourceOverrides ? oldSources: {}, } @ args: let # if dream lock is a file, read and parse it - dreamLock' = (utils.dreamLock.readDreamLock {inherit dreamLock;}).lock; + dreamLock' = (utils.dream-lock.readDreamLock {inherit dreamLock;}).lock; fetcher = if args.fetcher or null == null @@ -304,9 +304,9 @@ in let allOutputs, } @ args: let # inject dependencies - dreamLock = utils.dreamLock.injectDependencies args.dreamLock inject; + dreamLock = utils.dream-lock.injectDependencies args.dreamLock inject; - dreamLockInterface = (utils.dreamLock.readDreamLock {inherit dreamLock;}).interface; + dreamLockInterface = (utils.dream-lock.readDreamLock {inherit dreamLock;}).interface; produceDerivation = name: pkg: utils.applyOverridesToPackage { @@ -337,7 +337,7 @@ in let packageVersions ; - getSource = utils.dreamLock.getSource fetchedSources; + getSource = utils.dream-lock.getSource fetchedSources; }); # Makes the packages tree compatible with flakes schema. @@ -388,7 +388,7 @@ in let packageOverrides ? {}, } @ args: let # parse dreamLock - dreamLockLoaded = utils.dreamLock.readDreamLock {inherit (args) dreamLock;}; + dreamLockLoaded = utils.dream-lock.readDreamLock {inherit (args) dreamLock;}; dreamLock = dreamLockLoaded.lock; dreamLockInterface = dreamLockLoaded.interface; @@ -481,7 +481,7 @@ in let project // rec { dreamLock = - (utils.dreamLock.readDreamLock { + (utils.dream-lock.readDreamLock { dreamLock = "${toString config.projectRoot}/${project.dreamLockPath}"; }) .lock; diff --git a/src/lib/default.nix b/src/lib/default.nix index d24164f8..4878b9f0 100644 --- a/src/lib/default.nix +++ b/src/lib/default.nix @@ -17,6 +17,7 @@ listDirs listFiles mergeFlakes + mkFunction nameVersionPair prepareSourceTree readTextFile @@ -335,5 +336,13 @@ licenses = l.filter (license: license != null) _licenses; in licenses; + + mkFunction = {type, ...} @ attrs: + l.mkOption ( + attrs + // { + type = l.types.uniq (l.types.functionTo attrs.type); + } + ); in dlib diff --git a/src/modules/functions.translators/implementation.nix b/src/modules/functions.translators/implementation.nix index f9111e7f..df539889 100644 --- a/src/modules/functions.translators/implementation.nix +++ b/src/modules/functions.translators/implementation.nix @@ -52,7 +52,7 @@ # simpleTranslate2 puts dream-lock in result dreamLock = dreamLock'.result or dreamLock'; in - dream2nix.framework.utils.dreamLock.toJSON + dream2nix.framework.utils.dream-lock.toJSON # don't use nix to detect cycles, this will be more efficient in python (dreamLock // { _generic = builtins.removeAttrs dreamLock._generic [ \"cyclicDependencies\" ]; diff --git a/src/modules/top-level.nix b/src/modules/top-level.nix index 44535879..315c31a0 100644 --- a/src/modules/top-level.nix +++ b/src/modules/top-level.nix @@ -25,6 +25,11 @@ in { ./translators ./indexers ./utils + ./utils.translator + ./utils.index + ./utils.override + ./utils.toTOML + ./utils.dream-lock ]; options = { lib = lib.mkOption { diff --git a/src/modules/utils.dream-lock/default.nix b/src/modules/utils.dream-lock/default.nix new file mode 100644 index 00000000..274989a4 --- /dev/null +++ b/src/modules/utils.dream-lock/default.nix @@ -0,0 +1,6 @@ +{ + imports = [ + ./interface.nix + ./implementation.nix + ]; +} diff --git a/src/modules/utils/dream-lock.nix b/src/modules/utils.dream-lock/implementation.nix similarity index 98% rename from src/modules/utils/dream-lock.nix rename to src/modules/utils.dream-lock/implementation.nix index c1a2671a..269beeb8 100644 --- a/src/modules/utils/dream-lock.nix +++ b/src/modules/utils.dream-lock/implementation.nix @@ -6,7 +6,7 @@ dreamLock, newSourceRoot, } @ args: let - dreamLockLoaded = config.utils.dreamLock.readDreamLock {dreamLock = args.dreamLock;}; + dreamLockLoaded = config.utils.dream-lock.readDreamLock {dreamLock = args.dreamLock;}; iface = dreamLockLoaded.interface; patchVersion = version: source: if @@ -320,7 +320,7 @@ in json; in { - config.utils.dreamLock = { + config.utils.dream-lock = { inherit compressDreamLock decompressDreamLock diff --git a/src/modules/utils.dream-lock/interface.nix b/src/modules/utils.dream-lock/interface.nix new file mode 100644 index 00000000..0125f0fa --- /dev/null +++ b/src/modules/utils.dream-lock/interface.nix @@ -0,0 +1,35 @@ +{config, ...}: let + inherit (config.dlib) mkFunction; + l = config.lib // builtins; + t = l.types; +in { + options.utils.dream-lock = { + compressDreamLock = mkFunction { + type = t.attrs; + }; + decompressDreamLock = mkFunction { + type = t.attrs; + }; + getMainPackageSource = mkFunction { + type = t.attrs; + }; + getSource = mkFunction { + type = t.functionTo (t.functionTo (t.either t.package t.path)); + }; + getSubDreamLock = mkFunction { + type = t.functionTo (t.functionTo t.attrs); + }; + readDreamLock = mkFunction { + type = t.attrs; + }; + replaceRootSources = mkFunction { + type = t.attrs; + }; + injectDependencies = mkFunction { + type = t.functionTo t.attrs; + }; + toJSON = mkFunction { + type = t.attrs; + }; + }; +} diff --git a/src/modules/utils/index/build-script.py b/src/modules/utils.index/build-script.py similarity index 100% rename from src/modules/utils/index/build-script.py rename to src/modules/utils.index/build-script.py diff --git a/src/modules/utils.index/default.nix b/src/modules/utils.index/default.nix new file mode 100644 index 00000000..274989a4 --- /dev/null +++ b/src/modules/utils.index/default.nix @@ -0,0 +1,6 @@ +{ + imports = [ + ./interface.nix + ./implementation.nix + ]; +} diff --git a/src/modules/utils/index/default.nix b/src/modules/utils.index/implementation.nix similarity index 100% rename from src/modules/utils/index/default.nix rename to src/modules/utils.index/implementation.nix diff --git a/src/modules/utils.index/interface.nix b/src/modules/utils.index/interface.nix new file mode 100644 index 00000000..6130063f --- /dev/null +++ b/src/modules/utils.index/interface.nix @@ -0,0 +1,14 @@ +{config, ...}: let + inherit (config.dlib) mkFunction; + l = config.lib // builtins; + t = l.types; +in { + options.utils = { + generatePackagesFromLocksTree = mkFunction { + type = t.attrsOf t.package; + }; + makeOutputsForIndexes = mkFunction { + type = t.attrs; + }; + }; +} diff --git a/src/modules/utils/index/make-stats.py b/src/modules/utils.index/make-stats.py similarity index 100% rename from src/modules/utils/index/make-stats.py rename to src/modules/utils.index/make-stats.py diff --git a/src/modules/utils.override/default.nix b/src/modules/utils.override/default.nix new file mode 100644 index 00000000..274989a4 --- /dev/null +++ b/src/modules/utils.override/default.nix @@ -0,0 +1,6 @@ +{ + imports = [ + ./interface.nix + ./implementation.nix + ]; +} diff --git a/src/modules/utils/override.nix b/src/modules/utils.override/implementation.nix similarity index 100% rename from src/modules/utils/override.nix rename to src/modules/utils.override/implementation.nix diff --git a/src/modules/utils.override/interface.nix b/src/modules/utils.override/interface.nix new file mode 100644 index 00000000..a30f83a8 --- /dev/null +++ b/src/modules/utils.override/interface.nix @@ -0,0 +1,14 @@ +{config, ...}: let + inherit (config.dlib) mkFunction; + l = config.lib // builtins; + t = l.types; +in { + options.utils = { + applyOverridesToPackage = mkFunction { + type = t.attrs; + }; + loadOverridesDirs = mkFunction { + type = t.attrs; + }; + }; +} diff --git a/src/modules/utils.toTOML/default.nix b/src/modules/utils.toTOML/default.nix new file mode 100644 index 00000000..274989a4 --- /dev/null +++ b/src/modules/utils.toTOML/default.nix @@ -0,0 +1,6 @@ +{ + imports = [ + ./interface.nix + ./implementation.nix + ]; +} diff --git a/src/modules/utils/toTOML.nix b/src/modules/utils.toTOML/implementation.nix similarity index 100% rename from src/modules/utils/toTOML.nix rename to src/modules/utils.toTOML/implementation.nix diff --git a/src/modules/utils.toTOML/interface.nix b/src/modules/utils.toTOML/interface.nix new file mode 100644 index 00000000..adfbf606 --- /dev/null +++ b/src/modules/utils.toTOML/interface.nix @@ -0,0 +1,10 @@ +{config, ...}: let + l = config.lib // builtins; + t = l.types; +in { + options.utils = { + toTOML = config.dlib.mkFunction { + type = t.attrs; + }; + }; +} diff --git a/src/modules/utils.translator/default.nix b/src/modules/utils.translator/default.nix new file mode 100644 index 00000000..274989a4 --- /dev/null +++ b/src/modules/utils.translator/default.nix @@ -0,0 +1,6 @@ +{ + imports = [ + ./interface.nix + ./implementation.nix + ]; +} diff --git a/src/modules/utils/translator.nix b/src/modules/utils.translator/implementation.nix similarity index 100% rename from src/modules/utils/translator.nix rename to src/modules/utils.translator/implementation.nix diff --git a/src/modules/utils.translator/interface.nix b/src/modules/utils.translator/interface.nix new file mode 100644 index 00000000..8b2796ad --- /dev/null +++ b/src/modules/utils.translator/interface.nix @@ -0,0 +1,10 @@ +{config, ...}: let + l = config.lib // builtins; + t = l.types; +in { + options.utils = { + simpleTranslate = config.dlib.mkFunction { + type = t.functionTo t.attrs; + }; + }; +} diff --git a/src/modules/utils/implementation.nix b/src/modules/utils/implementation.nix index 60e63b2c..da568d59 100644 --- a/src/modules/utils/implementation.nix +++ b/src/modules/utils/implementation.nix @@ -216,13 +216,6 @@ script // {passthru = {inherit project;};}; }; in { - imports = [ - ./dream-lock.nix - ./override.nix - ./translator.nix - ./toTOML.nix - ./index - ]; config = { utils = impl; }; diff --git a/src/modules/utils/interface.nix b/src/modules/utils/interface.nix index 7c959eab..5715124f 100644 --- a/src/modules/utils/interface.nix +++ b/src/modules/utils/interface.nix @@ -1,14 +1,7 @@ {config, ...}: let + inherit (config.dlib) mkFunction; l = config.lib // builtins; t = l.types; - - mkFunction = {type, ...} @ attrs: - l.mkOption ( - attrs - // { - type = t.uniq (t.functionTo attrs.type); - } - ); in { options.utils = { scripts = { @@ -22,41 +15,9 @@ in { type = t.path; }; }; - dreamLock = { - compressDreamLock = mkFunction { - type = t.attrs; - }; - decompressDreamLock = mkFunction { - type = t.attrs; - }; - getMainPackageSource = mkFunction { - type = t.attrs; - }; - getSource = mkFunction { - type = t.functionTo (t.functionTo (t.either t.package t.path)); - }; - getSubDreamLock = mkFunction { - type = t.functionTo (t.functionTo t.attrs); - }; - readDreamLock = mkFunction { - type = t.attrs; - }; - replaceRootSources = mkFunction { - type = t.attrs; - }; - injectDependencies = mkFunction { - type = t.functionTo t.attrs; - }; - toJSON = mkFunction { - type = t.attrs; - }; - }; toDrv = mkFunction { type = t.package; }; - toTOML = mkFunction { - type = t.str; - }; hashPath = mkFunction { type = t.functionTo t.str; }; @@ -78,20 +39,5 @@ in { makeTranslateScript = mkFunction { type = t.package; }; - applyOverridesToPackage = mkFunction { - type = t.attrs; - }; - loadOverridesDirs = mkFunction { - type = t.attrs; - }; - simpleTranslate = mkFunction { - type = t.functionTo t.attrs; - }; - generatePackagesFromLocksTree = mkFunction { - type = t.attrsOf t.package; - }; - makeOutputsForIndexes = mkFunction { - type = t.attrs; - }; }; } diff --git a/src/subsystems/go/translators/gomod2nix/translate.nix b/src/subsystems/go/translators/gomod2nix/translate.nix index cecac1b6..85582849 100644 --- a/src/subsystems/go/translators/gomod2nix/translate.nix +++ b/src/subsystems/go/translators/gomod2nix/translate.nix @@ -68,4 +68,4 @@ }; }); in - dream2nix.framework.utils.dreamLock.toJSON translated + dream2nix.framework.utils.dream-lock.toJSON translated diff --git a/src/updaters/default.nix b/src/updaters/default.nix index 0f856a27..2323bfba 100644 --- a/src/updaters/default.nix +++ b/src/updaters/default.nix @@ -12,12 +12,12 @@ }: let inherit (framework) utils fetchers; - lockUtils = utils.dreamLock; + lockUtils = utils.dream-lock; updaters = callPackageDream ./updaters.nix {}; getUpdaterName = {dreamLock}: let - lock = (utils.dreamLock.readDreamLock {inherit dreamLock;}).lock; + lock = (utils.dream-lock.readDreamLock {inherit dreamLock;}).lock; source = lockUtils.getMainPackageSource lock; in lock.updater @@ -28,7 +28,7 @@ dreamLock, updater ? getUpdaterName {inherit dreamLock;}, }: let - lock = (utils.dreamLock.readDreamLock {inherit dreamLock;}).lock; + lock = (utils.dream-lock.readDreamLock {inherit dreamLock;}).lock; source = lockUtils.getMainPackageSource lock; updater' = updaters."${updater}"; in diff --git a/tests/unit/test_dream_lock.py b/tests/unit/test_dream_lock.py index 3d682319..3b20a397 100644 --- a/tests/unit/test_dream_lock.py +++ b/tests/unit/test_dream_lock.py @@ -26,7 +26,7 @@ exampleDreamLock = dict( def test_dream_lock_inject(): result = nix_ffi.callNixFunction( - 'framework.utils.dreamLock.injectDependencies', + 'framework.utils.dream-lock.injectDependencies', dreamLock=exampleDreamLock, inject=dict( example={ @@ -43,7 +43,7 @@ def test_dream_lock_inject(): def test_dream_lock_replace_root_sources(): result = nix_ffi.callNixFunction( - 'framework.utils.dreamLock.replaceRootSources', + 'framework.utils.dream-lock.replaceRootSources', dreamLock=exampleDreamLock, newSourceRoot=dict( type = "http",