{"id":14008414,"url":"https://github.com/fzakaria/mvn2nix","last_synced_at":"2025-08-08T02:10:31.486Z","repository":{"id":45157730,"uuid":"280964788","full_name":"fzakaria/mvn2nix","owner":"fzakaria","description":"Easily package your Maven Java application with the Nix package manager.","archived":false,"fork":false,"pushed_at":"2024-07-24T00:07:14.000Z","size":205,"stargazers_count":103,"open_issues_count":21,"forks_count":37,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-02T04:47:22.855Z","etag":null,"topics":["java","maven","nix","nixpkgs"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fzakaria.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["fzakaria"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2020-07-19T22:56:12.000Z","updated_at":"2025-04-02T01:32:23.000Z","dependencies_parsed_at":"2022-09-10T08:01:03.884Z","dependency_job_id":"2c33c1b5-b33d-46a8-82ff-16e43b274ce4","html_url":"https://github.com/fzakaria/mvn2nix","commit_stats":{"total_commits":55,"total_committers":9,"mean_commits":6.111111111111111,"dds":0.4727272727272728,"last_synced_commit":"88d4cdda093c7a6b3c09404d3b4c2aa0b3c1fd0c"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fzakaria/mvn2nix","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fzakaria%2Fmvn2nix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fzakaria%2Fmvn2nix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fzakaria%2Fmvn2nix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fzakaria%2Fmvn2nix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fzakaria","download_url":"https://codeload.github.com/fzakaria/mvn2nix/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fzakaria%2Fmvn2nix/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269351910,"owners_count":24402678,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-08T02:00:09.200Z","response_time":72,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","maven","nix","nixpkgs"],"created_at":"2024-08-10T11:01:39.287Z","updated_at":"2025-08-08T02:10:31.443Z","avatar_url":"https://github.com/fzakaria.png","language":"Java","funding_links":["https://github.com/sponsors/fzakaria"],"categories":["Java","包管理器"],"sub_categories":[],"readme":"__:warning: mvn2nix is seeking a maintainer. Please open an issue if you are interested.__\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"logo.png\" alt=\"mvn2nix\" width=\"164\" height=\"164\"\u003e\n\u003c/p\u003e\n\n![Build](https://github.com/fzakaria/mvn2nix/workflows/Build/badge.svg)\n[![built with nix](https://builtwithnix.org/badge.svg)](https://builtwithnix.org)\n\n\u003e Easily package your Maven Java application with the Nix package manager.\n\n\u003c!--ts--\u003e\n* [How to use this](#how-to-use-this)\n   * [Generating the Nix dependencies file](#generating-the-nix-dependencies-file)\n   * [Building a Maven repository](#building-a-maven-repository)\n   * [Sample Derivation](#sample-derivation)\n* [How does it work?](#how-does-it-work)\n* [Development](#development)\n\u003c!-- Added by: fmzakari, at: Mon Aug 24 21:40:20 PDT 2020 --\u003e\n\u003c!--te--\u003e\n\n__:warning: mvn2nix is in active development but is ready to be used. Please pin it to a commit to avoid\nany flux.__\n\n## How to use this\n\n```bash\n# non-flake\n$ nix run -f https://github.com/fzakaria/mvn2nix/archive/master.tar.gz --command mvn2nix\n\n# flake\n$ nix run github:fzakaria/mvn2nix#mvn2nix\n```\n\nIf you have [cachix](https://cachix.org/) installed, you can leverage our prebuilt binary.\n\u003e cachix use fzakaria\n\n### Generating the Nix dependencies file\n\nIn the same spirit of [bundix](https://github.com/nix-community/bundix), **mvn2nix** creates a Nix set with the\n*transitive closure* of all dependencies required by the application.\n\n```bash\n# non-flake usage\n$ nix run -f https://github.com/fzakaria/mvn2nix/archive/master.tar.gz \\\n        --command mvn2nix \u003e mvn2nix-lock.json\n\n# flake usage\n$ nix run github:fzakaria/mvn2nix#mvn2nix \u003e mvn2nix-lock.json\n\n$ head mvn2nix-lock.json\n{\n  \"dependencies\": {\n    \"org.junit.jupiter:junit-jupiter:jar:5.6.2\": {\n      \"layout\": \"org/junit/jupiter/junit-jupiter/5.6.2/junit-jupiter-5.6.2.jar\",\n      \"sha256\": \"dfc0d870dec4c5428a126ddaaa987bdaf8026cc27270929c9f26d52f3030ac61\",\n      \"url\": \"https://repo.maven.apache.org/maven2/org/junit/jupiter/junit-jupiter/5.6.2/junit-jupiter-5.6.2.jar\"\n    },\n    \"org.codehaus.plexus:plexus-utils:pom:3.0.15\": {\n      \"layout\": \"org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.pom\",\n      \"sha256\": \"b4fe0bed469e2e973c661b4b7647db374afee7bda513560e96cd780132308f0b\",\n      \"url\": \"https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.15/plexus-utils-3.0.15.pom\"\n    },\n```\n\nYou can then use this to download all the necessary dependencies to run your application.\n\n### Building a Maven repository\n\nNow that you have a **nix** dependencies file; we can re-construct a Maven repository using Nix!\n\n```nix\nlet mvn2nix = import (fetchTarball https://github.com/fzakaria/mvn2nix/archive/master.tar.gz) { };\nin\nmvn2nix.buildMavenRepositoryFromLockFile { file = ./mvn2nix-lock.json; }\n```\n\nThis creates a **/nix/store** path which is a Maven repository that can be used, such as in `mvn package --offline -Dmaven.repo.local=${mavenRepository}`\n\n```bash\n$ tree /nix/store/0ylsqi62jqz5gqf0dqrz5a3hj3jrzrwx-mvn2nix-repository | head\n\n/nix/store/0ylsqi62jqz5gqf0dqrz5a3hj3jrzrwx-mvn2nix-repository\n├── com\n│   └── google\n│       ├── code\n│       │   └── findbugs\n│       │       └── jsr305\n│       │           └── 3.0.2\n│       │               └── jsr305-3.0.2.jar -\u003e /nix/store/w20lb1dk730v77qis8l6sjqpljwkyql7-jsr305-3.0.2.jar\n│       ├── errorprone\n│       │   └── error_prone_annotations\n```\n\n### Sample Derivation\n\n```nix\n{ pkgs ? import \u003cnixpkgs\u003e {} }:\nlet\n  mvn2nix = import\n    (fetchTarball \"https://github.com/fzakaria/mvn2nix/archive/master.tar.gz\")\n    { };\n  mavenRepository =\n   mvn2nix.buildMavenRepositoryFromLockFile { file = ./mvn2nix-lock.json; };\ninherit (pkgs) lib stdenv jdk11_headless maven makeWrapper;\ninherit (stdenv) mkDerivation;\nin mkDerivation rec {\n  pname = \"my-artifact\";\n  version = \"0.01\";\n  name = \"${pname}-${version}\";\n  src = lib.cleanSource ./.;\n\n  nativeBuildInputs = [ jdk11_headless maven makeWrapper ];\n  buildPhase = ''\n    echo \"Building with maven repository ${mavenRepository}\"\n    mvn package --offline -Dmaven.repo.local=${mavenRepository}\n  '';\n\n  installPhase = ''\n    # create the bin directory\n    mkdir -p $out/bin\n\n    # create a symbolic link for the lib directory\n    ln -s ${mavenRepository} $out/lib\n\n    # copy out the JAR\n    # Maven already setup the classpath to use m2 repository layout\n    # with the prefix of lib/\n    cp target/${name}.jar $out/\n\n    # create a wrapper that will automatically set the classpath\n    # this should be the paths from the dependency derivation\n    makeWrapper ${jdk11_headless}/bin/java $out/bin/${pname} \\\n          --add-flags \"-jar $out/${name}.jar\"\n  '';\n}\n```\n\n### Example flake usage\n\n```nix\n# flake.nix\n{\n  inputs = {\n    mvn2nix.url = \"github:fzakaria/mvn2nix\";\n    utils.url = \"github:numtide/flake-utils\";\n  };\n\n  outputs = { nixpkgs, mvn2nix, utils, ... }:\n  let\n    pkgsForSystem = system: import nixpkgs {\n      # ./overlay.nix contains the logic to package local repository\n      overlays = [ mvn2nix.overlay (import ./overlay.nix) ];\n      inherit system;\n    };\n  in utils.lib.eachSystem utils.lib.defaultSystems (system: rec {\n    legacyPackages = pkgsForSystem system;\n    packages = utils.lib.flattenTree {\n      inherit (legacyPackages) myMavenProject;\n    };\n    defaultPackage = legacyPackages.myMavenProject;\n  });\n}\n```\n\n```nix\n# overlay.nix\nfinal: prev: {\n  myMavenProject = final.callPackage ./myPackage.nix { };\n}\n```\n\n```nix\n# myPackge.nix\n{ lib, stdenv, buildMavenRepositoryFromLockFile\n, makeWrapper, maven, jdk11_headless\n, nix-gitignore\n}:\n\n\nlet\n  mavenRepository = buildMavenRepositoryFromLockFile { file = ./mvn2nix-lock.json; };\nin stdenv.mkDerivation rec {\n  pname = \"myMavenProject\";\n  version = \"0.9.3\";\n  name = \"${pname}-${version}\";\n  src = nix-gitignore.gitignoreSource [ \"*.nix\" ] ./.;\n\n  nativeBuildInputs = [ jdk11_headless maven makeWrapper ];\n  buildPhase = ''\n    echo \"Building with maven repository ${mavenRepository}\"\n    mvn package --offline -Dmaven.repo.local=${mavenRepository}\n  '';\n\n  installPhase = ''\n    # create the bin directory\n    mkdir -p $out/bin\n\n    # create a symbolic link for the lib directory\n    ln -s ${mavenRepository} $out/lib\n\n    # copy out the JAR\n    # Maven already setup the classpath to use m2 repository layout\n    # with the prefix of lib/\n    cp target/${name}.jar $out/\n\n    # create a wrapper that will automatically set the classpath\n    # this should be the paths from the dependency derivation\n    makeWrapper ${jdk11_headless}/bin/java $out/bin/${pname} \\\n          --add-flags \"-jar $out/${name}.jar\"\n  '';\n}\n```\n\n## How does it work?\n\n**mvn2nix** relies on [maven-invoker](https://maven.apache.org/shared/maven-invoker/); which fires off\nMaven in a separate JVM process.\n\nMaven is executed with a temporary *ephemeral* local repository for the given goals provided (defaults to **package**).\nThe local repository is than traversed, and each encountered file is recorded in the dependencies list.\n\n**mvn2nix** includes an [example](examples/mvn2nix/default.nix) output \u0026 derivation that builds itself!\n\n## Development\n\nIf you are running *mvn2nix* from this repository, you can do so with **nix-build**\n\n```bash\n$ nix-build\n\n./result/bin/mvn2nix \u003e example/mvn2nix-lock.json\n```\n\nIf you want to test **buildMavenRepository** you can run:\n```bash\n$ nix-build -A buildMavenRepositoryFromLockFile --arg file \"./mvn2nix-lock.json\"\n```\n\nAt the moment, _mvn2nix_ is **self-bootstrapped** consuming it's own _mvn2nix-lock.json_.\nIf for whatever reason, the JSON format changes or you need to update the dependencies, you can run _mvn2nix-bootstrap_.\n\n```bash\n$ nix-build -A mvn2nix-bootstrap\n\n./result/bin/mvn2nix \u003e example/mvn2nix-lock.json\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffzakaria%2Fmvn2nix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffzakaria%2Fmvn2nix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffzakaria%2Fmvn2nix/lists"}