From 9dee6d3afc784434b22b8861a0ae03bf595c3a78 Mon Sep 17 00:00:00 2001 From: kp2pml30 Date: Mon, 12 May 2025 16:52:03 +0400 Subject: [PATCH] feat: add collecting nix derivation from source --- .gitignore | 2 ++ collect-sources.nix | 62 +++++++++++++++++++++++++++++++++++++++++++ flake.lock | 64 +++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 33 +++++++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 collect-sources.nix create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.gitignore b/.gitignore index 0416a3b..d3b2e3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /.ruff_cache __pycache__ /third-party +/.direnv +/.envrc diff --git a/collect-sources.nix b/collect-sources.nix new file mode 100644 index 0000000..cf049b9 --- /dev/null +++ b/collect-sources.nix @@ -0,0 +1,62 @@ +{ rootDerivation +, derivationName +, stdenv +, ... # added for future backwards compatibility +}: +let + config = builtins.fromJSON (builtins.readFile "${rootDerivation}/.git-third-party/config.json"); + allRepoNames = builtins.attrNames config.repos; + makeDependentDerivation = + name: + let + cleanName = builtins.head (builtins.tail (builtins.match "^(.*/|)([^/]+)$" name)); + myConfig = config.repos."${name}"; + shouldFetchModules = !(builtins.hasAttr "submodules" myConfig) || myConfig.submodules != []; + base-src = builtins.fetchGit { + url = myConfig.url; + rev = myConfig.commit; + submodules = shouldFetchModules; + #fetchSubmodules = shouldFetchModules; + # fetchLFS = true; + shallow = true; + }; + in stdenv.mkDerivation { + name = "git3party_${derivationName}_${cleanName}"; + src = base-src; + patches = + builtins.map + (num: "${rootDerivation}/.git-third-party/patches/${name}/${toString num}") + (builtins.genList (x: x + 1) myConfig.patches); + phases = [ "unpackPhase" "patchPhase" "installPhase" ]; + + #outputHashMode = "recursive"; + + installPhase = '' + cp -r . "$out" + ''; + }; + dependentDerivations = builtins.map (name: { name = name; der = makeDependentDerivation name; }) allRepoNames; + + cpFlags = "--no-preserve=ownership,mode --preserve=xattr"; + + copyLines = builtins.concatStringsSep "\n" ([ + "cp ${cpFlags} -r '${rootDerivation}/.' ." + ] ++ builtins.map (nameDer: "mkdir -p '${nameDer.name}'\ncp ${cpFlags} -r '${nameDer.der}'/. '${nameDer.name}'") dependentDerivations); +in + stdenv.mkDerivation { + name = derivationName; + + srcs = [ + rootDerivation + ] ++ (builtins.map (x: x.name) dependentDerivations); + + phases = [ "unpackPhase" "installPhase" ]; + + #outputHashMode = "recursive"; + + unpackPhase = copyLines; + + installPhase = '' + cp -r . "$out" + ''; + } diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..e8c2902 --- /dev/null +++ b/flake.lock @@ -0,0 +1,64 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": [ + "systems" + ] + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1744440957, + "narHash": "sha256-FHlSkNqFmPxPJvy+6fNLaNeWnF1lZSgqVCl/eWaJRc4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "26d499fc9f1d567283d5d56fcf367edd815dba1d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-24.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "systems": "systems" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..50cde64 --- /dev/null +++ b/flake.nix @@ -0,0 +1,33 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11"; + systems = { + url = "github:nix-systems/default"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + flake-utils = { + url = "github:numtide/flake-utils"; + inputs.systems.follows = "systems"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = inputs@{ self, nixpkgs, flake-utils, ... }: + flake-utils.lib.eachDefaultSystem + (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + python312 + pre-commit + ]; + + shellHook = '' + ''; + }; + } + ); +}