improve flake interfaces and apps

- improve exposing apps for other flakes
  - configure overridesDirs via config
This commit is contained in:
DavHau 2021-11-06 16:06:34 +07:00
parent 39db511926
commit 60d89f776f
7 changed files with 128 additions and 74 deletions

View File

@ -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

View File

@ -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) {};

View File

@ -1,3 +0,0 @@
{
}

View File

@ -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 =

View File

@ -6,17 +6,12 @@
nixpkgsSrc ? <nixpkgs>,
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

32
src/utils/config.nix Normal file
View File

@ -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;
}

View File

@ -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))