diff --git a/flake.nix b/flake.nix index b36d8684..e94e53b5 100644 --- a/flake.nix +++ b/flake.nix @@ -65,20 +65,11 @@ }; # create a directory containing the files listed in externalPaths - makeExternalDir = pkgs: pkgs.runCommand "dream2nix-external" {} - (lib.concatStringsSep "\n" - (lib.mapAttrsToList - (inputName: paths: - lib.concatStringsSep "\n" - (lib.forEach - paths - (path: '' - mkdir -p $out/${inputName}/$(dirname ${path}) - cp ${inp."${inputName}"}/${path} $out/${inputName}/${path} - ''))) - externalPaths)); + makeExternalDir = import ./src/utils/external-dir.nix; - externalDirFor = forAllSystems (system: makeExternalDir); + externalDirFor = forAllSystems (system: pkgs: makeExternalDir { + inherit externalPaths externalSources pkgs; + }); # An interface to access files of external projects. # This implementation aceeses the flake inputs directly, @@ -95,7 +86,10 @@ # system specific dream2nix api dream2nixFor = forAllSystems (system: pkgs: import ./src rec { externalDir = externalDirFor."${system}"; - inherit externalSources lib overridesDirs pkgs; + inherit externalSources lib pkgs; + config = { + inherit overridesDirs; + }; }); in @@ -112,7 +106,7 @@ # Similar to drem2nixFor but will require 'system(s)' or 'pkgs' as an argument. # Produces flake-like output schema. lib = (import ./src/lib.nix { - inherit externalSources overridesDirs lib; + inherit externalPaths externalSources lib; nixpkgsSrc = "${nixpkgs}"; }) # system specific dream2nix library @@ -133,12 +127,7 @@ # all apps including cli, install, etc. apps = forAllSystems (system: pkgs: - lib.mapAttrs (appName: app: - { - type = "app"; - program = b.toString app.program; - } - ) dream2nixFor."${system}".apps.apps + dream2nixFor."${system}".apps.flakeApps ); # a dev shell for working on dream2nix diff --git a/src/apps/default.nix b/src/apps/default.nix index 83b7ba63..1ec45d57 100644 --- a/src/apps/default.nix +++ b/src/apps/default.nix @@ -1,16 +1,30 @@ { + lib, pkgs, callPackageDream, translators, ... }: + +let + b = builtins; +in + rec { apps = { inherit cli contribute install; dream2nix = cli; }; + flakeApps = + lib.mapAttrs (appName: app: + { + type = "app"; + program = b.toString app.program; + } + ) apps; + # the unified translator cli cli = callPackageDream (import ./cli) {}; diff --git a/src/config.json b/src/config.json deleted file mode 100644 index 0db3279e..00000000 --- a/src/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - -} diff --git a/src/default.nix b/src/default.nix index 3595b760..c607e677 100644 --- a/src/default.nix +++ b/src/default.nix @@ -12,39 +12,51 @@ ${pkgs.nixUnstable}/bin/nix --option experimental-features "nix-command flakes" "$@" '', + # default to empty dream2nix config + config ? + # if called via CLI, load cnfig via env + if builtins ? getEnv && builtins.getEnv "d2nConfigFile" != "" then + builtins.toPath (builtins.getEnv "d2nConfigFile") + # load from default directory + else + {}, + # dependencies of dream2nix externalSources ? lib.genAttrs (lib.attrNames (builtins.readDir externalDir)) (inputName: "${externalDir}/${inputName}"), + # will be defined if called via flake + externalPaths ? null, + # required for non-flake mode externalDir ? + # if flake is used, construct external dir from flake inputs + if externalPaths != null then + (import ./utils/external-dir.nix { + inherit externalPaths externalSources pkgs; + }) # if called via CLI, load externals via env - if builtins ? getEnv && builtins.getEnv "d2nExternalDir" != "" then + else if builtins ? getEnv && builtins.getEnv "d2nExternalDir" != "" then builtins.getEnv "d2nExternalDir" # load from default directory else ./external, - # dream2nix overrides - overridesDirs ? - # if called via CLI, load externals via env - if builtins ? getEnv && builtins.getEnv "d2nOverridesDirs" != "" then - lib.splitString ":" (builtins.getEnv "d2nOverridesDirs") - # load from default directory - else - [ ./overrides ], -}: +}@args: let b = builtins; + config = (import ./utils/config.nix).loadConfig args.config or {}; + # like pkgs.callPackage, but includes all the dream2nix modules callPackageDream = f: args: pkgs.callPackage f (args // { inherit builders; inherit callPackageDream; + inherit config; inherit externals; inherit externalSources; inherit fetchers; @@ -57,8 +69,6 @@ let utils = callPackageDream ./utils {}; - config = builtins.fromJSON (builtins.readFile ./config.json); - # apps for CLI and installation apps = callPackageDream ./apps {}; @@ -88,7 +98,7 @@ let }; dreamOverrides = - utils.loadOverridesDirs overridesDirs pkgs; + utils.loadOverridesDirs config.overridesDirs pkgs; # the location of the dream2nix framework for self references (update scripts, etc.) dream2nixWithExternals = diff --git a/src/lib.nix b/src/lib.nix index cd57f76f..003dc848 100644 --- a/src/lib.nix +++ b/src/lib.nix @@ -6,17 +6,12 @@ nixpkgsSrc ? , lib ? (import nixpkgsSrc {}).lib, - # (if called impurely ./default.nix will handle externals and overrides) - externalSources ? null, + # default to empty dream2nix config + config ? {}, - # dream2nix overrides - overridesDirs ? - # if called via CLI, load externals via env - if builtins ? getEnv && builtins.getEnv "d2nOverridesDirs" != "" then - lib.splitString ":" (builtins.getEnv "d2nOverridesDirs") - # load from default directory - else - [ ./overrides ], + externalSources, + + externalPaths, }@args: @@ -24,6 +19,13 @@ let b = builtins; + config = (import ./utils/config.nix).loadConfig args.config or {}; + + dream2nixForSystem = system: pkgs: + import ./default.nix + { inherit config externalPaths externalSources pkgs; }; + + # TODO: design output schema for cross compiled packages makePkgsKey = pkgs: let @@ -75,28 +77,18 @@ let { pkgs ? null, systems ? [], - overridesDirs ? [], + config ? {}, }@argsInit: let - overridesDirs' = args.overridesDirs ++ argsInit.overridesDirs or []; + overridesDirs' = config.overridesDirs; allPkgs = makeNixpkgs pkgs systems; - dream2nixFor = - lib.mapAttrs - (system: pkgs: - import ./default.nix - ( - { - inherit pkgs; - overridesDirs = overridesDirs'; - } - // (lib.optionalAttrs (externalSources != null) { - inherit externalSources; - }) - )) - allPkgs; + forAllSystems = f: + lib.mapAttrs f allPkgs; + + dream2nixFor = forAllSystems dream2nixForSystem; in { riseAndShine = riseAndShineArgs: @@ -117,6 +109,17 @@ let (allOutputs: output: lib.recursiveUpdate allOutputs output) {} flakifiedOutputs; + + apps = + forAllSystems + (system: pkgs: + dream2nixFor."${system}".apps.flakeApps); + + defaultApp = + forAllSystems + (system: pkgs: + dream2nixFor."${system}".apps.flakeApps.dream2nix); + }; riseAndShine = @@ -132,17 +135,7 @@ let allPkgs = makeNixpkgs pkgs systems; dream2nixFor = - lib.mapAttrs - (system: pkgs: - import ./default.nix - ({ inherit pkgs; } - // (lib.optionalAttrs (externalSources != null) { - inherit externalSources; - }) - // (lib.optionalAttrs (overridesDirs != null) { - inherit overridesDirs; - }))) - allPkgs; + lib.mapAttrs dream2nixForSystem allPkgs; allBuilderOutputs = lib.mapAttrs diff --git a/src/utils/config.nix b/src/utils/config.nix new file mode 100644 index 00000000..4f66aac1 --- /dev/null +++ b/src/utils/config.nix @@ -0,0 +1,32 @@ +let + + b = builtins; + + # loads attrs either from s: + # - json file + # - json string + # - attrset (no changes) + loadAttrs = input: + if b.isPath input then + b.fromJSON (b.readFile input) + else if b.isString input then + b.fromJSON input + else if b.isAttrs input then + input + else + throw "input for loadAttrs must be json file or string or attrs"; + + # load dream2nix config extending with defaults + loadConfig = configInput: + let + config = loadAttrs configInput; + defaults = { + overridesDirs = []; + packagesDir = null; + }; + in + defaults // config; +in +{ + inherit loadConfig; +} \ No newline at end of file diff --git a/src/utils/external-dir.nix b/src/utils/external-dir.nix new file mode 100644 index 00000000..08acc601 --- /dev/null +++ b/src/utils/external-dir.nix @@ -0,0 +1,19 @@ +{ + lib ? pkgs.lib, + pkgs, + externalSources, + externalPaths, +}: + +pkgs.runCommand "dream2nix-external-dir" {} + (lib.concatStringsSep "\n" + (lib.mapAttrsToList + (inputName: paths: + lib.concatStringsSep "\n" + (lib.forEach + paths + (path: '' + mkdir -p $out/${inputName}/$(dirname ${path}) + cp ${externalSources."${inputName}"}/${path} $out/${inputName}/${path} + ''))) + externalPaths))