Skip to content

Commit

Permalink
initial commit of nested-partition-loader
Browse files Browse the repository at this point in the history
This include a workaround for cargo-vendor being unable to
vendor compiler's dependencies.

It will load rust's Cargo.lock and manually version every component in a
nix derivation.

Both project and rust cargo vendor are then merged together (with
symlinks) and provided for cargo to use.

Signed-off-by: Arthur Gautier <baloo@superbaloo.net>
  • Loading branch information
baloo committed Oct 21, 2021
1 parent 6355e24 commit e108c50
Show file tree
Hide file tree
Showing 15 changed files with 593 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/cargo-lock-utils/compute-checksum
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python

import json
import sys
import os
import hashlib

source, checksum = sys.argv[1:3]

BLOCKSIZE=65536
crate_files = {}

for root, dirs, files in os.walk(source):
for f in files:
if f in ['.gitattributes', '.gitignore', '.cargo-ok', '.cargo-checksum.json', '.cargo_vcs_info.json']:
continue
if f.endswith('.rej') or f.endswith('.orig'):
continue

fpath = os.path.join(root, f)
rpath = os.path.relpath(fpath, source)

hasher = hashlib.sha256()
with open(fpath, 'rb') as fp:
buf = fp.read(BLOCKSIZE)
while len(buf) > 0:
hasher.update(buf)
buf = fp.read(BLOCKSIZE)

crate_files[rpath] = hasher.hexdigest()

output = {
"files": crate_files,
"package": checksum
}

out = json.dumps(output, separators=(',', ':'), sort_keys=True)

sys.stdout.write(out)

15 changes: 15 additions & 0 deletions packages/cargo-lock-utils/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{ pkgs ? import <nixpkgs> {}
}:

with pkgs.python3Packages;

buildPythonApplication {
pname = "merge-cargo-lock";
version = "0.0.1";

propagatedBuildInputs = [
toml
];

src = ./.;
}
14 changes: 14 additions & 0 deletions packages/cargo-lock-utils/list-cargo-lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python

import toml
import sys

source = sys.argv[1]

with open(source) as f:
content = toml.load(f)

# first rewrite rust dependencies to make sure they are version pinned
for p in content['package']:
if p.get('source') == 'registry+https://github.com/rust-lang/crates.io-index':
print("https://crates.io/api/v1/crates/%s/%s/download %s-%s %s" % (p['name'], p['version'], p['name'], p['version'], p['checksum']))
63 changes: 63 additions & 0 deletions packages/cargo-lock-utils/merge-cargo-lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python

import toml
import sys

source, rust = sys.argv[1:3]

with open(source) as f:
origin_content = toml.load(f)

with open(rust) as f:
rust_content = toml.load(f)

# first rewrite rust dependencies to make sure they are version pinned
package_versions = {}
for p in rust_content['package']:
if p['name'] in package_versions:
package_versions[p['name']].append(p['version'])
else:
package_versions[p['name']] = [p['version']]

for p in rust_content['package']:
if 'dependencies' in p:
deps = []
for dep in p['dependencies']:
if ' ' in dep:
deps.append(dep)
else:
deps.append(dep + " " + package_versions[dep][0])
p['dependencies'] = deps

# do the same with the origin ones
origin_packages = set()
package_versions = {}
for p in origin_content['package']:
origin_packages.add((p['name'], p['version']))

if p['name'] in package_versions:
package_versions[p['name']].append(p['version'])
else:
package_versions[p['name']] = [p['version']]

for p in origin_content['package']:
if 'dependencies' in p:
deps = []
for dep in p['dependencies']:
if ' ' in dep:
deps.append(dep)
else:
deps.append(dep + " " + package_versions[dep][0])
p['dependencies'] = deps


# then merge the rust in the origin ones
# to do that, we can't add the same dependency twice.
# TODO: do we need to care about the dependencies? (make it a super set?)
for p in rust_content['package']:
tup = (p['name'], p['version'])
if tup not in origin_packages:
origin_content['package'].append(p)

output = toml.dumps(origin_content)
print(output)
13 changes: 13 additions & 0 deletions packages/cargo-lock-utils/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python

from setuptools import setup, find_packages

setup(name='cargo-lock-utils',
version='0.0.1',
packages=find_packages(),
scripts=[
'compute-checksum',
'list-cargo-lock',
'merge-cargo-lock',
],
)
3 changes: 3 additions & 0 deletions packages/nested-partition-loader/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[unstable]
build-std = ["core", "compiler_builtins", "alloc"]
build-std-features = [ "compiler-builtins-mem" ]
1 change: 1 addition & 0 deletions packages/nested-partition-loader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
111 changes: 111 additions & 0 deletions packages/nested-partition-loader/Cargo.lock

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

16 changes: 16 additions & 0 deletions packages/nested-partition-loader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "nested-partition-loader"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
uefi = { path = "uefi-rs", features = [ "exts", "logger" ] }
uefi-services = { path = "uefi-rs/uefi-services" }

log = { version = "0.4.11", default-features = false }

[patch.crates-io]
uefi-macros = { path = "uefi-rs/uefi-macros" }
uefi = { path = "uefi-rs" }
17 changes: 17 additions & 0 deletions packages/nested-partition-loader/cargo-debug-hook.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cargoRewriteConfigHook() {
echo "LASJKDLKAJSD"
cat .cargo/config

set -x
echo @mergeCargoLock@

# cat >> .cargo/config <<EOF
#[source."library"]
#"directory" = "/nix/store/iz8h5l1q4r8dqg88rwqapyi1p4ygcfv4-rust-src/lib/rustlib/src/rust/library/"
#EOF
cat .cargo/config
}

if [ -z "${dontCargoRewriteConfig-}" ]; then
postUnpackHooks+=(cargoRewriteConfigHook)
fi
Loading

4 comments on commit e108c50

@osa1
Copy link

@osa1 osa1 commented on e108c50 Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@baloo, thanks for the workaround for the cargo vendor bug, it works great!

I'm curious about the licensing of this code. I checked the repository (baloo/nixos-firmware) but it doesn't have any LICENSE files as far as I can see. Can we use this code on Apache 2.0 licensed open-source code? Would it be possible for you to add a LICENSE file to the nixos-firmware project?

I'm not an expert in OS licensing, but I think when a code does not have a LICENSE file then you have to assume the worst and cannot use the code.

@baloo
Copy link
Owner Author

@baloo baloo commented on e108c50 Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@osa1 I don't know much about Apache2.0, but I'd be comfortable pushing a bsd-2-clause license if that works for you.

also, please mind I'm doing "tricks" with the checksums. I believe the model is correct since I'm trusting the checksum already available in the Cargo.lock which is then used to verify the checksums.json file but, no guarantees, please review that carefully.

@baloo
Copy link
Owner Author

@baloo baloo commented on e108c50 Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also please tag me again, so that it pops up in my notification and I don't forget :D)

@baloo
Copy link
Owner Author

@baloo baloo commented on e108c50 Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.