Skip to content

Commit 08a5262

Browse files
committed
add package for nixos-anywhere
1 parent 9351882 commit 08a5262

File tree

4 files changed

+114
-11
lines changed

4 files changed

+114
-11
lines changed

pyproject.toml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "nixos-anywhere-pxe"
7+
description = "Install NixOS with PXE"
8+
dynamic = ["version"]
9+
scripts = { nixos-anywhere-pxe = "nixos_anywhere_pxe:main"}
10+
11+
[tool.pytest.ini_options]
12+
addopts = "--cov . --cov-report term --cov-fail-under=100 --no-cov-on-fail"
13+
14+
[tool.mypy]
15+
python_version = "3.10"
16+
warn_redundant_casts = true
17+
disallow_untyped_calls = true
18+
disallow_untyped_defs = true
19+
no_implicit_optional = true
20+
21+
[[tool.mypy.overrides]]
22+
module = "setuptools.*"
23+
ignore_missing_imports = true
24+
25+
[[tool.mypy.overrides]]
26+
module = "pytest.*"
27+
ignore_missing_imports = true
28+
29+
[tool.ruff]
30+
line-length = 88
31+
32+
select = ["E", "F", "I"]
33+
ignore = [ "E501" ]
34+
35+
[tool.black]
36+
line-length = 88
37+
target-version = ['py310']
38+
include = '\.pyi?$'
39+
exclude = '''
40+
/(
41+
\.git
42+
| \.hg
43+
| \.mypy_cache
44+
| \.tox
45+
| \.venv
46+
| _build
47+
| buck-out
48+
| build
49+
| dist
50+
# The following are specific to Black, you probably don't want those.
51+
| blib2to3
52+
| tests/data
53+
| profiling
54+
)/
55+
'''

src/flake-module.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
perSystem = { config, pkgs, ... }: {
33
packages = {
44
nixos-anywhere = pkgs.callPackage ./. { };
5+
nixos-anywhere-pxe = pkgs.callPackage ./nixos_anywhere_pxe { };
56
default = config.packages.nixos-anywhere;
67
};
78
devShells.default = config.packages.nixos-anywhere.devShell;

nixos-anywhere-pxe.py renamed to src/nixos_anywhere_pxe/__init__.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import argparse
44
import binascii
55
import gzip
6+
import ipaddress
67
import json
78
import os
89
import shutil
@@ -15,12 +16,12 @@
1516
from tempfile import TemporaryDirectory
1617
from typing import IO, Iterator, List, NoReturn, Optional, Tuple, Union
1718

18-
from netaddr import AddrFormatError, IPAddress, IPNetwork
19-
2019
FILE = Union[None, int, IO]
2120

2221
NIXOS_ANYWHERE_SH = Path(__file__).parent.absolute() / "src/nixos-anywhere.sh"
2322

23+
24+
2425
def run(
2526
cmd: Union[str, list[str]],
2627
input: Optional[str] = None,
@@ -92,7 +93,7 @@ def next_dhcp_event(self) -> Iterator[DhcpEvent]:
9293
# even if dnsmasq is configured to change UID to an unprivileged user.
9394
@contextmanager
9495
def start_dnsmasq(
95-
interface: str, dhcp_range: Tuple[IPAddress, IPAddress]
96+
interface: str, dhcp_range: Tuple[ipaddress.IPv4Address, ipaddress.IPv4Address]
9697
) -> Iterator[Dnsmasq]:
9798
with TemporaryDirectory(prefix="dnsmasq.") as _temp:
9899
temp = Path(_temp)
@@ -151,7 +152,7 @@ def start_dnsmasq(
151152

152153
@contextmanager
153154
def start_pixiecore(
154-
server_ip: IPAddress,
155+
server_ip: ipaddress.IPv4Address,
155156
port: int,
156157
ssh_public_key: Path,
157158
pxe_image_store_path: Path,
@@ -225,9 +226,9 @@ class Options:
225226
flake: str
226227
netboot_image_flake: str
227228
dhcp_interface: str
228-
dhcp_server_ip: IPAddress
229+
dhcp_server_ip: ipaddress.IPv4Address
229230
dhcp_subnet: int
230-
dhcp_range: Tuple[IPAddress, IPAddress]
231+
dhcp_range: Tuple[ipaddress.IPv4Address, ipaddress.IPv4Address]
231232
pixiecore_http_port: int
232233
pause_after_completion: bool
233234
nixos_anywhere_args: List[str]
@@ -276,16 +277,16 @@ def parse_args(args: list[str]) -> Options:
276277

277278
parsed, unknown_args = parser.parse_known_args(args)
278279
try:
279-
dhcp_subnet = IPNetwork(parsed.dhcp_subnet)
280-
except AddrFormatError as e:
280+
dhcp_subnet = ipaddress.ip_network(parsed.dhcp_subnet)
281+
except ValueError as e:
281282
die(f"subnet specified in --dhcp-subnet is not valid: {e}")
282283

283284
if dhcp_subnet.version != 4:
284285
die(
285286
"Sorry. Only ipv4 subnets are supported just now because of compatibility with older bios firmware"
286287
)
287288

288-
hosts = dhcp_subnet.iter_hosts()
289+
hosts = dhcp_subnet.hosts()
289290
try:
290291
dhcp_server_ip = next(hosts)
291292
except StopIteration:
@@ -384,13 +385,14 @@ def build_pxe_image(netboot_image_flake: str) -> Path:
384385
)
385386
return Path(res.stdout.strip())
386387

388+
387389
def pause():
388390
print("")
389391
# no clue how to flush stdin with python. Gonna wait for a specific string instead (as opposed to wait for [enter]).
390392
answer = ""
391-
while (answer != "continue"):
393+
while answer != "continue":
392394
answer = input(
393-
"Answer 'continue' to terminate this script and tear down the network to the server: "
395+
"Answer 'continue' to terminate this script and tear down the network to the server: "
394396
)
395397

396398

src/nixos_anywhere_pxe/default.nix

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{ pkgs
2+
, lib
3+
, python3
4+
, ruff
5+
, runCommand
6+
}:
7+
let
8+
src = ../..;
9+
10+
devDependencies = lib.attrValues {
11+
inherit (pkgs) ruff;
12+
inherit (python3.pkgs)
13+
black
14+
mypy
15+
pytest
16+
pytest-cov
17+
pytest-subprocess
18+
setuptools
19+
wheel
20+
;
21+
};
22+
23+
package = python3.pkgs.buildPythonPackage {
24+
name = "nixos-anywhere-pxe";
25+
inherit src;
26+
format = "pyproject";
27+
nativeBuildInputs = [
28+
python3.pkgs.setuptools
29+
];
30+
passthru.tests = { inherit nixos-anywhere-pxe-mypy; };
31+
passthru.devDependencies = devDependencies;
32+
};
33+
34+
checkPython = python3.withPackages (_ps: devDependencies);
35+
36+
nixos-anywhere-pxe-mypy = runCommand "nixos-anywhere-pxe-mypy" { } ''
37+
cp -r ${src} ./src
38+
chmod +w -R ./src
39+
cd src
40+
${checkPython}/bin/mypy .
41+
touch $out
42+
'';
43+
44+
in
45+
package

0 commit comments

Comments
 (0)