Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to configure the default builder #106

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ default are mandatory, extra attributes are passed to **mkDerivation**):
- **enableLeiningen**: Makes Leiningen accessible at build time (Default:
`false`)

- **builder-extra-inputs**: Extra inputs to the default builder (Default: `[ ]`)

- **builder-java-opts** List of Java options to include in default builder
command (Default: `[ ]`)

- **builder-preBuild** Pre build commands for the default builder (Default:
`""`)
- **builder-postBuild** Post build commands for the default builder (Default:
`""`)

**Example**:

```nix
Expand Down
3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@
overlays.default = final: prev:
let common = final.callPackage ./pkgs/common.nix { }; in
{
inherit (final.callPackage ./pkgs/cljApps.nix { }) clj-builder deps-lock;
deps-lock = final.callPackage ./pkgs/depsLock.nix { inherit common; };
clj-builder = final.callPackage ./pkgs/cljBuilder.nix { inherit common; };
mk-deps-cache = final.callPackage ./pkgs/mkDepsCache.nix;
mkCljBin = final.callPackage ./pkgs/mkCljBin.nix { inherit common; };
mkCljLib = final.callPackage ./pkgs/mkCljLib.nix { };
Expand Down
21 changes: 16 additions & 5 deletions helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ in

cfg = _m.config;

cljDrv = pkgs'.mkCljBin {
jdkRunner = cfg.jdk;
inherit (cfg) projectSrc name version main-ns buildCommand
lockfile java-opts compileCljOpts javacOpts;
};
in

assert (pkgs'.lib.assertMsg
Expand All @@ -64,6 +59,22 @@ in
"Leiningen is incompatible with Clojure tools.build options (compileCljOpts and javacOpts)"
);

assert (pkgs'.lib.assertMsg
(cfg.buildCommand != null ->
cfg.builder-extra-inputs == [ ] && cfg.builder-java-opts == [ ]
&& cfg.builder-preBuild == "" && cfg.builder-postBuild == ""
)
"buildCommand and builder-* options are incompatible"
);

let
cljDrv = pkgs'.mkCljBin {
jdkRunner = cfg.jdk;
inherit (cfg) projectSrc name version main-ns buildCommand
lockfile java-opts compileCljOpts javacOpts
builder-extra-inputs builder-java-opts builder-preBuild builder-postBuild;
};
in

if cfg.customJdk.enable then
pkgs'.customJdk
Expand Down
26 changes: 25 additions & 1 deletion modules/top-level.nix
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,33 @@ let types = lib.types; in
buildCommand = lib.mkOption {
default = null;
type = types.nullOr types.str;
description = "Command to build the jar application. If not provided, a default builder is used";
description = lib.mdDoc "Command to build the jar application. If not provided, a default builder is used";
};

builder-extra-inputs = lib.mkOption {
default = [ ];
type = types.listOf types.package;
description = lib.mdDoc "Extra inputs to the default builder";
};

builder-java-opts = lib.mkOption {
type = types.listOf types.str;
default = [ ];
description = lib.mdDoc "List of Java options to include in default builder command";
};

builder-preBuild = lib.mkOption {
default = "";
type = types.str;
description = lib.mdDoc "Pre build commands for the default builder";
};
builder-postBuild = lib.mkOption {
default = "";
type = types.str;
description = lib.mdDoc "Post build commands for the default builder";
};


lockfile = lib.mkOption {
default = null;
type = types.nullOr types.str;
Expand Down
56 changes: 0 additions & 56 deletions pkgs/cljApps.nix

This file was deleted.

16 changes: 16 additions & 0 deletions pkgs/cljBuilder.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ common
, leiningen
, jdk
, extra-runtime-inputs ? [ ]
, java-opts ? [ ]
, preBuild ? ""
, postBuild ? ""
}:

common.writeCljApplication {
name = "clj-builder";
runtimeInputs = [ leiningen jdk ] ++ extra-runtime-inputs;
clj-main = "cljnix.builder-cli";
classpath = "${../src}:${common.internal-deps-classpath}";
inherit java-opts preBuild postBuild;
}
45 changes: 44 additions & 1 deletion pkgs/common.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
{ runtimeShell, writeText }:
{ lib
, runtimeShell
, writeText
, fetchurl
, fetchgit
, writeShellApplication
}:

let
lock = builtins.fromJSON (builtins.readFile ./builder-lock.json);
in

{
binaryTemplate =
writeText "template" ''
Expand All @@ -7,4 +18,36 @@
exec "@jdk@/bin/java" @javaOpts@ \
-jar "@jar@" "$@"
'';

internal-deps-classpath =
let
deps = builtins.concatMap
(dep:
if !(dep ? rev) then # maven dep
let src = fetchurl { inherit (dep) url hash; }; in
[ "${src}" ]
else # git dep
let src = fetchgit { inherit (dep) url hash rev; }; in
builtins.map (x: "${src}/${x}") dep.paths
)
lock;
in
builtins.concatStringsSep ":" deps;

writeCljApplication =
{ name, runtimeInputs, clj-main, classpath, java-opts, preBuild, postBuild }:

writeShellApplication {
inherit name runtimeInputs;
text =
preBuild +
''
java \
${lib.escapeShellArgs java-opts} \
"-Dclojure.tools.logging.factory=clojure.tools.logging.impl/slf4j-factory" \
"-classpath" "${classpath}" \
clojure.main -m ${clj-main} "$@"
'' +
postBuild;
};
}
16 changes: 16 additions & 0 deletions pkgs/depsLock.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ common
, leiningen
, jdk
, jq
}:

common.writeCljApplication {
name = "deps-lock";
runtimeInputs = [ jq leiningen ];
clj-main = "cljnix.core";
classpath = "${../src}:${common.internal-deps-classpath}";

java-opts = [ ];
preBuild = "";
postBuild = "";
}
16 changes: 15 additions & 1 deletion pkgs/mkCljBin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
, compileCljOpts ? null
, javacOpts ? null
, enableLeiningen ? false
, builder-extra-inputs ? [ ]
, builder-java-opts ? [ ]
, builder-preBuild ? ""
, builder-postBuild ? ""

# Needed for version ranges
# TODO maybe we can find a better solution?
Expand All @@ -50,6 +54,10 @@ let
"nativeBuildInputs"
"compileCljOpts"
"javacOpts"
"builder-extra-inputs"
"builder-java-opts"
"builder-preBuild"
"builder-postBuild"
];

deps-cache = mk-deps-cache {
Expand Down Expand Up @@ -78,7 +86,13 @@ stdenv.mkDerivation ({
++
[
jdkRunner
clj-builder
(clj-builder.override {
jdk = jdkRunner;
extra-runtime-inputs = builder-extra-inputs;
java-opts = builder-java-opts;
preBuild = builder-preBuild;
postBuild = builder-postBuild;
})
]
++ (lib.lists.optional (! isNull buildCommand) (clojure.override { jdk = jdkRunner; }))
++ (lib.lists.optional enableLeiningen leiningen);
Expand Down