Skip to content

Commit dd300fd

Browse files
authored
Merge pull request #8 from Golem-Base/rvdp/uv
feat: migrate to uv
2 parents ae71ed5 + b529f4b commit dd300fd

16 files changed

Lines changed: 2514 additions & 159 deletions

File tree

.envrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
use flake
1+
use flake . --builders ""

.github/workflows/example.yml

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
name: Python Golem Base SDK CI
2+
13
on:
24
push:
35
branches:
@@ -8,53 +10,58 @@ on:
810
workflow_dispatch:
911

1012
jobs:
11-
run-python-example:
13+
ci:
1214
runs-on: ubuntu-latest
1315

14-
#services:
15-
# gb-op-geth:
16-
# image: "quay.io/golemnetwork/gb-op-geth:latest"
17-
# ports:
18-
# - 8545:8545
19-
# options: |
20-
2116
steps:
22-
- name: Run a gb-op-geth container
23-
run: >
24-
docker run -d -p 8545:8545 -e GITHUB_ACTIONS=true -e CI=true
25-
quay.io/golemnetwork/gb-op-geth:latest --dev --http --http.api
26-
'eth,web3,net,debug,golembase' --verbosity 3 --http.addr '0.0.0.0' --http.port 8545
27-
--http.corsdomain '*' --http.vhosts '*' --ws --ws.addr '0.0.0.0' --ws.port 8545
28-
2917
- name: Check out the SDK repo
3018
uses: actions/checkout@v4
3119

20+
- name: Install Nix
21+
uses: cachix/install-nix-action@v31
22+
23+
- name: Build the SDK
24+
run: >
25+
nix build .#golem-base-sdk --no-write-lock-file
26+
27+
- name: Build the SDK dist
28+
run: >
29+
nix build .#golem-base-sdk.dist --no-write-lock-file
30+
31+
- name: Build the example SDK app
32+
run: >
33+
nix build .#golem-base-sdk-example --no-write-lock-file --reference-lock-file
34+
../flake.lock
35+
working-directory: ./example
36+
3237
- name: Check out the gb-op-geth repo
3338
uses: actions/checkout@v4
3439
with:
3540
repository: Golem-Base/golembase-op-geth
3641
path: gb-op-geth
3742

38-
- name: Install Nix
39-
uses: cachix/install-nix-action@v31
40-
4143
- name: Set up Go
4244
uses: actions/setup-go@v5
4345
with:
4446
go-version-file: "gb-op-geth/go.mod"
4547
cache-dependency-path: |
4648
gb-op-geth/go.sum
4749
50+
- name: Run a gb-op-geth container
51+
run: >
52+
docker run -d -p 8545:8545 -e GITHUB_ACTIONS=true -e CI=true
53+
quay.io/golemnetwork/gb-op-geth:latest --dev --http --http.api
54+
'eth,web3,net,debug,golembase' --verbosity 3 --http.addr '0.0.0.0' --http.port 8545
55+
--http.corsdomain '*' --http.vhosts '*' --ws --ws.addr '0.0.0.0' --ws.port 8545
56+
4857
- name: Create and fund an account
4958
run: |
5059
go run ./cmd/golembase account create
5160
go run ./cmd/golembase account fund
5261
working-directory: ./gb-op-geth
5362

54-
- name: Build the example SDK app
55-
run: nix build .#golem-base-sdk-example --no-write-lock-file
56-
working-directory: ./example
57-
5863
- name: Run the example SDK app
59-
run: ./result/bin/main --instance local
64+
run: >
65+
nix develop --no-write-lock-file --reference-lock-file ../flake.lock --command
66+
./result/bin/main --instance local
6067
working-directory: ./example

example/.envrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
use flake
1+
use flake . --reference-lock-file ../flake.lock --no-write-lock-file --builders ""

example/.uv-links

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/nix/store/8s2f649jy6gdc831xj5pcvrgnscffncb-uv-links

example/flake.nix

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
11
{
22
inputs = {
3-
golem-base-sdk.url = "git+file:..";
3+
nixpkgs.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz";
44

55
devshell = {
66
url = "github:numtide/devshell";
77
inputs = {
8-
nixpkgs.follows = "golem-base-sdk/nixpkgs";
8+
nixpkgs.follows = "nixpkgs";
99
};
1010
};
1111

1212
blueprint = {
1313
url = "github:numtide/blueprint";
1414
inputs = {
15-
nixpkgs.follows = "golem-base-sdk/nixpkgs";
15+
nixpkgs.follows = "nixpkgs";
16+
};
17+
};
18+
19+
pyproject-nix = {
20+
url = "github:pyproject-nix/pyproject.nix";
21+
inputs.nixpkgs.follows = "nixpkgs";
22+
};
23+
24+
uv2nix = {
25+
url = "github:pyproject-nix/uv2nix";
26+
inputs = {
27+
pyproject-nix.follows = "pyproject-nix";
28+
nixpkgs.follows = "nixpkgs";
29+
};
30+
};
31+
32+
pyproject-build-systems = {
33+
url = "github:pyproject-nix/build-system-pkgs";
34+
inputs = {
35+
pyproject-nix.follows = "pyproject-nix";
36+
uv2nix.follows = "uv2nix";
37+
nixpkgs.follows = "nixpkgs";
1638
};
1739
};
1840
};

example/golem_base_sdk_example/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ async def run_example(instance: str) -> None: # noqa: PLR0915
217217
)
218218

219219
await watch_logs_handle.unsubscribe()
220+
221+
logger.info("""\n
222+
*********************************************
223+
* Creating an entity to test unsubscribe... *
224+
*********************************************
225+
""")
226+
227+
create_receipt = await client.create_entities(
228+
[GolemBaseCreate(b"hello", 60, [Annotation("app", "demo")], [])]
229+
)
230+
entity_key = create_receipt[0].entity_key
231+
logger.info("receipt: %s", create_receipt)
232+
logger.info("entity count: %s", await client.get_entity_count())
233+
234+
logger.info("created entity with key %s", entity_key)
235+
logger.info("storage value: %s", await client.get_storage_value(entity_key))
236+
metadata = await client.get_entity_metadata(entity_key)
237+
logger.info("entity metadata: %s", metadata)
238+
239+
await client.delete_entities([GolemBaseDelete(entity_key)])
220240
else:
221241
logger.warning("Could not connect to the API...")
222242

example/nix/devshell.nix

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
1-
{ pkgs, perSystem, ... }:
1+
{
2+
pkgs,
3+
perSystem,
4+
...
5+
}:
6+
7+
let
8+
inherit (perSystem.self.golem-base-sdk-example.passthru) virtualenvDev;
9+
in
210

311
perSystem.devshell.mkShell {
412
packages = [
5-
(pkgs.python3.withPackages (ps: [
6-
ps.anyio
7-
ps.pyxdg
8-
perSystem.golem-base-sdk.golem-base-sdk
9-
]))
13+
virtualenvDev
14+
pkgs.uv
15+
];
16+
17+
env = [
18+
{
19+
# Don't create venv using uv
20+
name = "UV_NO_SYNC";
21+
value = "1";
22+
}
23+
{
24+
# Force uv to use Python interpreter from venv
25+
name = "UV_PYTHON";
26+
value = "${virtualenvDev}/bin/python";
27+
}
28+
{
29+
# Prevent uv from downloading managed Python's
30+
name = "UV_PYTHON_DOWNLOADS";
31+
value = "never";
32+
}
1033
];
34+
35+
devshell.startup.uv2nix.text =
36+
# bash
37+
''
38+
# Undo dependency propagation by nixpkgs.
39+
unset PYTHONPATH
40+
# Get repository root using git. This is expanded at runtime by the editable `.pth` machinery.
41+
export REPO_ROOT=$(git rev-parse --show-toplevel)
42+
'';
1143
}
Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,83 @@
11
{
22
pkgs,
3-
pname,
4-
perSystem,
3+
inputs,
54
...
65
}:
76

87
let
98
inherit (pkgs) lib;
109

11-
types-pyxdg =
12-
let
13-
pname = "types-pyxdg";
14-
version = "0.28.0.20240106";
15-
in
16-
pkgs.python3Packages.buildPythonPackage {
17-
inherit pname version;
18-
format = "pyproject";
10+
# We need an actual path here, because of the editable install used below
11+
src = ../..;
1912

20-
src = pkgs.fetchPypi {
21-
inherit pname version;
22-
hash = "sha256-UF/GOG2MCl0KkUzrK3bo4tvQsRAhK1s5Xb6xyUg+24g=";
23-
};
24-
25-
nativeBuildInputs = [ pkgs.python3Packages.setuptools ];
26-
};
13+
workspace = inputs.uv2nix.lib.workspace.loadWorkspace {
14+
workspaceRoot = src;
15+
};
2716

28-
in
17+
overlay = workspace.mkPyprojectOverlay {
18+
sourcePreference = "wheel";
19+
};
2920

30-
pkgs.python3Packages.buildPythonPackage {
31-
inherit pname;
32-
version = "0.0.1";
21+
pythonSet =
22+
(pkgs.callPackage inputs.pyproject-nix.build.packages {
23+
python = pkgs.python312;
24+
}).overrideScope
25+
(
26+
lib.composeManyExtensions [
27+
inputs.pyproject-build-systems.overlays.default
28+
overlay
29+
]
30+
);
3331

34-
format = "pyproject";
32+
inherit (pkgs.callPackages inputs.pyproject-nix.build.util { }) mkApplication;
3533

36-
src = lib.fileset.toSource {
37-
root = ../..;
38-
fileset = lib.fileset.unions (
39-
map (lib.path.append ../..) [
40-
"pyproject.toml"
41-
"golem_base_sdk_example"
42-
"README.md"
43-
]
44-
);
34+
editableOverlay = workspace.mkEditablePyprojectOverlay {
35+
# This env var needs to be set to the root of the repo in the shell that runs the
36+
# application, otherwise python will not import golem_base_sdk from the right place
37+
root = "$REPO_ROOT/example";
38+
# Only enable editable for this package
39+
members = [ "golem-base-sdk" ];
4540
};
4641

47-
nativeBuildInputs = [
48-
pkgs.python3Packages.flit-core
49-
];
42+
editablePythonSet = pythonSet.overrideScope (
43+
lib.composeManyExtensions [
44+
editableOverlay
5045

51-
buildInputs = [
52-
types-pyxdg
53-
];
46+
# Apply fixups for building an editable package of your workspace packages
47+
(final: prev: {
48+
golem-base-sdk = prev.golem-base-sdk.overrideAttrs (old: {
49+
# We need the editables build system added here
50+
nativeBuildInputs =
51+
old.nativeBuildInputs
52+
++ final.resolveBuildSystem {
53+
editables = [ ];
54+
};
55+
});
56+
})
57+
]
58+
);
5459

55-
propagatedBuildInputs = [
56-
pkgs.python3Packages.anyio
57-
pkgs.python3Packages.pyxdg
58-
perSystem.golem-base-sdk.golem-base-sdk
59-
];
60+
virtualenv = editablePythonSet.mkVirtualEnv "golem-base-sdk-example-env" workspace.deps.default;
61+
virtualenvDev = editablePythonSet.mkVirtualEnv "golem-base-sdk-example-env" workspace.deps.all;
62+
in
6063

61-
nativeCheckInputs = [
62-
pkgs.mypy
63-
pkgs.pylint
64-
pkgs.ruff
65-
];
64+
mkApplication {
65+
venv = virtualenv;
66+
package = pythonSet.golem-base-sdk-example.overrideAttrs (prevAttrs: {
67+
nativeCheckInputs = [
68+
pkgs.mypy
69+
pkgs.ruff
70+
];
6671

67-
checkPhase = ''
68-
mypy golem_base_sdk_example
69-
ruff check --no-cache golem_base_sdk_example
70-
'';
72+
doCheck = true;
7173

72-
meta = with lib; {
73-
homepage = "";
74-
description = "";
75-
license = licenses.gpl3Only;
76-
platforms = platforms.linux ++ platforms.darwin;
77-
};
74+
checkPhase = ''
75+
mypy golem_base_sdk_example
76+
ruff check --no-cache golem_base_sdk_example
77+
'';
78+
79+
passthru = (prevAttrs.passthru or { }) // {
80+
inherit virtualenv virtualenvDev;
81+
};
82+
});
7883
}

0 commit comments

Comments
 (0)