Skip to content

Commit

Permalink
Merge pull request #496 from nix-community/v1-compat
Browse files Browse the repository at this point in the history
feat(v1): init compat module for pure legacy translators
  • Loading branch information
DavHau authored Mar 26, 2023
2 parents 046dfad + f96eb79 commit 76863ca
Show file tree
Hide file tree
Showing 11 changed files with 2,212 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/modules/flake-parts/makeOutputsArgs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

# TODO(antotocar34) make a smart enum of all available translators conditional on the given the subsystem? Is this possible?
translator = mkOption {
description = "Translators to use";
description = "Translator to use";
example = ["yarn-lock" "package-json"];
type = t.str;
};
Expand Down
3 changes: 2 additions & 1 deletion src/modules/utils.override/implementation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
loadOverrides = dir:
l.genAttrs (config.dlib.dirNames dir) (name:
import (dir + "/${name}") {
inherit (config) lib pkgs;
inherit pkgs;
inherit (config) lib;
satisfiesSemver = constraint: pkg:
config.utils.satisfiesSemver pkg.version constraint;
});
Expand Down
2 changes: 1 addition & 1 deletion src/modules/utils/implementation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
writeScriptBin
;

poetry2nixSemver = import "${config.externalSources.poetry2nix}/semver.nix" {
poetry2nixSemver = import ./poetry2nix/semver.nix {
inherit (config) lib;
# copied from poetry2nix
ireplace = idx: value: list: (
Expand Down
1,821 changes: 1,821 additions & 0 deletions src/modules/utils/poetry2nix/LICENSE

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions src/modules/utils/poetry2nix/semver.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{ lib, ireplace }:
let
inherit (builtins) elemAt match;
operators =
let
matchWildCard = s: match "([^*])(\\.[*])" s;
mkComparison = ret: version: v: builtins.compareVersions version v == ret;
mkIdxComparison = idx: version: v:
let
ver = builtins.splitVersion v;
minor = builtins.toString (lib.toInt (elemAt ver idx) + 1);
upper = builtins.concatStringsSep "." (ireplace idx minor ver);
in
operators.">=" version v && operators."<" version upper;
dropWildcardPrecision = f: version: constraint:
let
m = matchWildCard constraint;
hasWildcard = m != null;
c = if hasWildcard then (elemAt m 0) else constraint;
v =
if hasWildcard then (builtins.substring 0 (builtins.stringLength c) version)
else version;
in
f v c;
in
{
# Prefix operators
"==" = dropWildcardPrecision (mkComparison 0);
">" = dropWildcardPrecision (mkComparison 1);
"<" = dropWildcardPrecision (mkComparison (-1));
"!=" = v: c: ! operators."==" v c;
">=" = v: c: operators."==" v c || operators.">" v c;
"<=" = v: c: operators."==" v c || operators."<" v c;
# Semver specific operators
"~" = mkIdxComparison 1;
"^" = mkIdxComparison 0;
"~=" = v: c:
let
# Prune constraint
parts = builtins.splitVersion c;
pruned = lib.take ((builtins.length parts) - 1) parts;
upper = builtins.toString (
(lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1
);
upperConstraint = builtins.concatStringsSep "." (ireplace (builtins.length pruned - 1) upper pruned);
in
operators.">=" v c && operators."<" v upperConstraint;
# Infix operators
"-" = version: v: operators.">=" version v.vl && operators."<=" version v.vu;
# Arbitrary equality clause, just run simple comparison
"===" = v: c: v == c;
#
};
re = {
operators = "([=><!~^]+)";
version = "([0-9.*x]+)";
};
parseConstraint = constraint:
let
constraintStr = builtins.replaceStrings [ " " ] [ "" ] constraint;
# The common prefix operators
mPre = match "${re.operators} *${re.version}" constraintStr;
# There is also an infix operator to match ranges
mIn = match "${re.version} *(-) *${re.version}" constraintStr;
in
(
if mPre != null then {
op = elemAt mPre 0;
v = elemAt mPre 1;
}
# Infix operators are range matches
else if mIn != null then {
op = elemAt mIn 1;
v = {
vl = (elemAt mIn 0);
vu = (elemAt mIn 2);
};
}
else throw "Constraint \"${constraintStr}\" could not be parsed"
);
satisfiesSemver = version: constraint:
let
inherit (parseConstraint constraint) op v;
in
if constraint == "*" then true else operators."${op}" version v;
in
{ inherit satisfiesSemver; }
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
python $installDeps
echo "Symlinking transitive executables to $nodeModules/.bin"
for dep in ${lib.toString nodeDeps}; do
for dep in ${toString nodeDeps}; do
binDir=$dep/lib/node_modules/.bin
if [ -e $binDir ]; then
for bin in $(ls $binDir/); do\
Expand Down
1 change: 1 addition & 0 deletions treefmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ excludes = [
"overrides/nodejs/electron/*",
"src/templates/translators/*",
"src/modules/_template/*",
"src/modules/utils/poetry2nix/semver.nix",
]

[formatter.py]
Expand Down
161 changes: 161 additions & 0 deletions v1/nix/modules/drv-parts/dream2nix-legacy/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
{
config,
options,
extendModules,
lib,
drv-parts,
...
}: let
l = lib // builtins;

cfg = config.dream2nix-legacy;

pkgs = drv-parts.inputs.nixpkgs.legacyPackages.x86_64-linux;

dlib =
(l.evalModules {
modules = [
../../../../../src/modules/dlib
../../../../../src/modules/dlib.construct
../../../../../src/modules/dlib.parsing
../../../../../src/modules/dlib.simpleTranslate2
libModule
];
})
.config
.dlib;

utils =
(l.evalModules {
modules = [
../../../../../src/modules/utils
../../../../../src/modules/utils.override
../../../../../src/modules/utils.toTOML
../../../../../src/modules/utils.translator
../../../../../src/modules/dlib
libModule
];
})
.config
.utils;

legacy-interface =
(l.evalModules {
modules = [
../../../../../src/modules/dream2nix-interface
../../../../../src/modules/dlib
../../../../../src/modules/fetchers
../../../../../src/modules/functions.default-fetcher
../../../../../src/modules/functions.fetchers
../../../../../src/modules/utils
../../../../../src/modules/utils.dream-lock
../../../../../src/modules/utils.override
buildersModule
configModule
libModule
pkgsModule
];
})
.config
.dream2nix-interface;

buildersModule = {
options.buildersBySubsystem = l.mkOption {type = l.types.raw;};
config.buildersBySubsystem = buildersBySubsystem;
};

configModule = {
options.dream2nixConfig = l.mkOption {type = l.types.raw;};
config.dream2nixConfig = {
overridesDirs = [../../../../../overrides];
};
};

libModule = {
options.lib = l.mkOption {type = l.types.raw;};
config.lib = lib // builtins;
};

pkgsModule = {
options.pkgs = l.mkOption {type = l.types.raw;};
config.pkgs = pkgs;
};

buildersBySubsystem.${cfg.subsystem}.default = import (../../../../.. + "/src/subsystems/${cfg.subsystem}/builders/${cfg.builder}/default.nix") {
inherit lib pkgs utils;
dlib = {};
};

translator =
(l.evalModules {
modules = [
../../../../../src/modules/interfaces.translator/interface.nix
(../../../../.. + "/src/subsystems/${cfg.subsystem}/translators/${cfg.translator}")
libModule
];
specialArgs = {
inherit dlib utils;
};
})
.config;

tree = dlib.prepareSourceTree {inherit (cfg) source;};

project = {
inherit (config) name;
relPath = cfg.relPath;
subsystemInfo = cfg.subsystemInfo;
};

result =
translator.translate
(cfg.subsystemInfo
// {
inherit project tree;
inherit (cfg) source;
});

dreamLock = result.result or result;

defaultSourceOverride = dreamLock: let
defaultPackage = dreamLock._generic.defaultPackage;
defaultPackageVersion =
dreamLock._generic.packages."${defaultPackage}";
in {
"${defaultPackage}"."${defaultPackageVersion}" = "${cfg.source}/${dreamLock._generic.location}";
};

dreamOverrides = let
overridesDirs = [../../../../../overrides];
in
utils.loadOverridesDirs overridesDirs pkgs;

outputs = legacy-interface.makeOutputsForDreamLock {
inherit dreamLock;
sourceRoot = cfg.source;
sourceOverrides = oldSources:
dlib.recursiveUpdateUntilDepth
1
(defaultSourceOverride dreamLock)
(cfg.sourceOverrides oldSources);
packageOverrides =
l.recursiveUpdate
(dreamOverrides."${dreamLock._generic.subsystem}" or {})
(cfg.packageOverrides or {});
};

drvModule = drv-parts.lib.makeModule {
packageFunc = outputs.packages.default;
};

# hacky call drvModule manually to prevent infinite recursions
eval = drvModule {
inherit config options extendModules;
};
in {
imports = [
./interface.nix
];

public = l.mkForce eval.config.public;
}
67 changes: 67 additions & 0 deletions v1/nix/modules/drv-parts/dream2nix-legacy/interface.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
config,
lib,
...
}: let
l = lib // builtins;
t = l.types;
in {
options.dream2nix-legacy = {
builder = l.mkOption {
description = "Builder to use";
example = ["build-rust-package"];
type = t.str;
};
relPath = l.mkOption {
default = "";
description = "Relative path to project tree from source";
type = t.str;
};
source = l.mkOption {
type = t.either t.path t.package;
description = "Source of the package to build with dream2nix";
};
subsystem = l.mkOption {
description = ''Name of subsystem to use. Examples: rust, python, nodejs'';
example = "nodejs";
type = t.str;
};
subsystemInfo = l.mkOption {
default = {};
description = "Translator specific arguments";
type = t.lazyAttrsOf (t.anything);
};
translator = l.mkOption {
description = "Translator to use";
example = ["yarn-lock" "package-json"];
type = t.str;
};

# overrides
packageOverrides = l.mkOption {
default = {};
type = t.lazyAttrsOf t.attrs;
description = "Overrides to customize build logic for dependencies or top-level packages";
};
sourceOverrides = l.mkOption {
default = old: {};
type = t.functionTo (t.lazyAttrsOf (t.listOf t.package));
description = ''
Override the sources of dependencies or top-level packages.
For more details, refer to
https://nix-community.github.io/dream2nix/intro/override-system.html
'';
example = l.literalExpression ''
oldSources: {
bar."13.2.0" = builtins.fetchTarball {
url = "https://example.com/example.tar.gz";
sha256 = "sha256-0000000000000000000000000000000000000000000=";
};
baz."1.0.0" = builtins.fetchTarball {
url = "https://example2.com/example2.tar.gz";
sha256 = "sha256-0000000000000000000000000000000000000000000=";
};
'';
};
};
}
Loading

0 comments on commit 76863ca

Please sign in to comment.