Skip to content

Commit

Permalink
git-repo: update to 2.45, patch to run locally
Browse files Browse the repository at this point in the history
The `repo` tool derivation by default includes only the main wrapper script
which fetches the actual tool sources from the internet. We modify the
derivation to provide default local sources patched with support for repo2nix,
unless specified otherwise with CLI parameters.

Flake compat was updated and nixpkgs-unstable re-introduced to facilitate this
change.
  • Loading branch information
jaen authored and Atemu committed Jun 17, 2024
1 parent 40ec5a1 commit 9ed6761
Show file tree
Hide file tree
Showing 24 changed files with 1,005 additions and 20 deletions.
35 changes: 34 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";

androidPkgs.url = "github:tadfisher/android-nixpkgs/stable";

flake-compat.url = "github:nix-community/flake-compat";
};

outputs = { self, nixpkgs, androidPkgs, ... }@inputs: let
outputs = { self, nixpkgs, nixpkgs-unstable, androidPkgs, flake-compat, ... }@inputs: let
pkgs = import ./pkgs/default.nix { inherit inputs; };
in {
# robotnixSystem evaluates a robotnix configuration
Expand All @@ -24,6 +28,7 @@

packages.x86_64-linux = {
manual = (import ./docs { inherit pkgs; }).manual;
gitRepo = pkgs.gitRepo;
};

devShell.x86_64-linux = pkgs.mkShell {
Expand Down
17 changes: 17 additions & 0 deletions flake/compat.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{ system ? builtins.currentSystem }:
let
lock = builtins.fromJSON (builtins.readFile ./../flake.lock);
flake-compat-entry = lock.nodes.root.inputs.flake-compat;

inherit (lock.nodes."${ flake-compat-entry }".locked) owner repo narHash;

flake-compat = builtins.fetchTarball {
url = "https://github.com/${ owner }/${ repo }/archive/${ lock.nodes.flake-compat.locked.rev }.tar.gz";
sha256 = narHash;
};
in
import flake-compat {
inherit system;

src = ./..;
}
15 changes: 6 additions & 9 deletions pkgs/default.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# SPDX-FileCopyrightText: 2020 Daniel Fullmer and robotnix contributors
# SPDX-License-Identifier: MIT

{ inputs ? (import (
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/12c64ca55c1014cdc1b16ed5a804aa8576601ff2.tar.gz";
sha256 = "0jm6nzb83wa6ai17ly9fzpqc40wg1viib8klq8lby54agpl213w5"; }
) {
src = ../.;
}).defaultNix.inputs,
... }@args:
{
system ? builtins.currentSystem,
inputs ? (import ../flake/compat.nix { inherit system; }).defaultNix.inputs,
...
}@args:

let
inherit (inputs) nixpkgs androidPkgs;
Expand All @@ -17,5 +14,5 @@ in nixpkgs.legacyPackages.x86_64-linux.appendOverlays [
androidPkgs.packages = androidPkgs.packages.x86_64-linux;
androidPkgs.sdk = androidPkgs.sdk.x86_64-linux;
})
(import ./overlay.nix)
(import ./overlay.nix { inherit inputs; })
]
70 changes: 70 additions & 0 deletions pkgs/gitRepo/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{ lib, inputs, fetchFromGitHub, rsync, git, gnupg, less, openssh, ... }:
let
inherit (inputs) nixpkgs-unstable;

unstablePkgs = nixpkgs-unstable.legacyPackages.x86_64-linux;
in
unstablePkgs.gitRepo.overrideAttrs(oldAttrs: rec {
version = "2.45";

src = fetchFromGitHub {
owner = "android";
repo = "tools_repo";
rev = "v${ version }";
hash = "sha256-f765TcOHL8wdPa9qSmGegofjCXx1tF/K5bRQnYQcYVc=";
};

nativeBuildInputs = (oldAttrs.nativeBuildInputs or []) ++ [ rsync git ];

repo2nixPatches = ./patches;

# NOTE: why `git apply` instead of relying on `patches`? For some reason when
# using `patches` the source `rsync`ed into `var/repo` is missing those changes
installPhase = ''
runHook preInstall
mkdir -p var/repo
rsync -a $src/ var/repo/
(
export GIT_CONFIG_GLOBAL=$TMPDIR/.gitconfig
export GIT_CONFIG_NOSYSTEM=true
cd var/repo
git config --global --add safe.directory "$PWD"
git config --global user.email "nemo@nix"
git config --global user.name "Nemo Nix"
chmod +w -R .
git init
git add -A
git commit -m "Upstream sources"
git am $repo2nixPatches/*.patch
git log -n 1 --format="%H" > ../../COMMITED_REPO_REV
)
mkdir -p $out/var/repo
mkdir -p $out/bin
rsync -a var/repo/ $out/var/repo/
# Copying instead of symlinking to the above directory is necessary, because otherwise
# running `repo init` fails, as I assume the script gets confused by being located in
# a git repo itself
cp repo $out/bin/repo
runHook postInstall
'';

# Specify the patched checkout as the default version of repo
postFixup = ''
wrapProgram "$out/bin/repo" \
--set REPO_URL "file://$out/var/repo" \
--set REPO_REV "$(cat ./COMMITED_REPO_REV)" \
--prefix PATH ":" "${ lib.makeBinPath [ git gnupg less openssh ] }"
'';
})
137 changes: 137 additions & 0 deletions pkgs/gitRepo/patches/0001-add-initial-hacky-version-of-repo2nix.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
From 8fe8a2efaa6bcd404c256a29d1dc5ed4ae241a08 Mon Sep 17 00:00:00 2001
From: ajs124 <[email protected]>
Date: Tue, 26 Feb 2019 04:48:13 +0100
Subject: [PATCH 01/16] add initial (hacky) version of "repo2nix"

now with less impurities
---
project.py | 4 +-
subcmds/nix.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 102 insertions(+), 2 deletions(-)
create mode 100644 subcmds/nix.py

diff --git a/project.py b/project.py
index 1f5e4c3..c3c3384 100644
--- a/project.py
+++ b/project.py
@@ -2887,8 +2887,8 @@ class Project:
)

def _LsRemote(self, refs):
- cmd = ["ls-remote", self.remote.name, refs]
- p = GitCommand(self, cmd, capture_stdout=True)
+ cmd = ["ls-remote", self.remote.url, refs]
+ p = GitCommand(self, cmd, cwd="/", capture_stdout=True)
if p.Wait() == 0:
return p.stdout
return None
diff --git a/subcmds/nix.py b/subcmds/nix.py
new file mode 100644
index 0000000..f113ede
--- /dev/null
+++ b/subcmds/nix.py
@@ -0,0 +1,100 @@
+#
+# Copyright (C) 2008 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import print_function
+
+from pyversion import is_python3
+if is_python3():
+ import http.cookiejar as cookielib
+ import urllib.error
+ import urllib.parse
+ import urllib.request
+ import xmlrpc.client
+else:
+ import imp
+ import urllib2
+ import urlparse
+ import xmlrpclib
+ urllib = imp.new_module('urllib')
+ urllib.error = urllib2
+ urllib.parse = urlparse
+ urllib.request = urllib2
+ xmlrpc = imp.new_module('xmlrpc')
+ xmlrpc.client = xmlrpclib
+
+try:
+ import threading as _threading
+except ImportError:
+ import dummy_threading as _threading
+
+try:
+ import resource
+ def _rlimit_nofile():
+ return resource.getrlimit(resource.RLIMIT_NOFILE)
+except ImportError:
+ def _rlimit_nofile():
+ return (256, 256)
+
+try:
+ import multiprocessing
+except ImportError:
+ multiprocessing = None
+
+from command import Command, MirrorSafeCommand
+
+class Nix(Command, MirrorSafeCommand):
+ common = True
+ helpSummary = "Export nix file with sources"
+ helpUsage = """
+%prog [<project>...]
+"""
+ helpDescription = """
+"""
+
+ def Execute(self, opt, args):
+ all_projects = self.GetProjects(args, missing_ok=True, submodules_ok=False)
+
+ oS = '{\n'
+ oS += "unpackPhase = ''\n" \
+ 'echo "reassembling source tree from git source store paths"\n' \
+ 'mkdir src; cd src\n' \
+ 'for src in $srcs; do\n' \
+ " dest_folder=$(stripHash $src); dest_folder=''${dest_folder//=//}\n" \
+ ' echo "$src -> $dest_folder"\n' \
+ ' mkdir -p "$dest_folder"\n' \
+ ' cp --reflink=auto --no-preserve=ownership --no-dereference --preserve=links --recursive "$src/." "$dest_folder/"\n' \
+ ' chmod -R u+w "$dest_folder"\n' \
+ 'done\n' \
+ 'echo "creating symlinks and copies as specified in repo manifest(s)"\n'
+ for p in all_projects:
+ for f in p.linkfiles:
+ oS += 'ln -s ' + f.src_rel_to_dest + ' ' + f.dest + '\n'
+ for c in p.copyfiles:
+ oS += 'cp --reflink=auto ' + p.relpath + '/' + c.src + ' ' + c.dest + '\n'
+ oS += "'';\n"
+
+ oS += 'sources = [\n'
+ for p in all_projects:
+ oS += ' (builtins.fetchGit {\n'
+ oS += ' url = "' + p.remote.url + '";\n'
+ if 'refs/heads' in p.revisionExpr:
+ oS += ' ref = "' + p.revisionExpr.split('/')[-1] + '";\n'
+ else:
+ oS += ' ref = "' + p.revisionExpr + '";\n'
+ oS += ' rev = "' + p._LsRemote(p.revisionExpr).split('\t')[0] + '";\n'
+ oS += ' name = "' + p.relpath.replace('/', '=') + '";\n'
+ oS += ' })\n'
+ oS += '];\n}'
+ print(oS)
\ No newline at end of file
--
2.44.0

Loading

0 comments on commit 9ed6761

Please sign in to comment.