Skip to content

Commit

Permalink
nix flakes
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann Bahl committed Aug 27, 2023
1 parent c35d3ad commit 08c74d8
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 45 deletions.
23 changes: 19 additions & 4 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ on:
branches: [ main ]

jobs:
build:

check-poetry:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Install poetry
Expand All @@ -26,8 +24,25 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install liblzo2-dev libsystemd-dev
sudo apt-get install liblzo2-dev
poetry install
- name: Test with pytest
run: |
poetry run pytest
check-nix:
runs-on: ubuntu-latest
steps:
- name: git checkout
uses: actions/checkout@v3
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Check Nixpkgs inputs
uses: DeterminateSystems/flake-checker-action@main
with:
fail-mode: true

- name: check flake
run: |
nix flake check -L
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ tags
vagrant/.vagrant
.pytest_cache
share/
/venv
4 changes: 2 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ Backy has switched to using `poetry` to manage its dependencies. This means
that you can use `poetry install` to install dependencies from PyPI.

If you don't have `backy` in your PATH when developing, enter the poetry
virtualenv with `poetry shell` or if you're using nix with `nix-shell`.
virtualenv with `poetry shell` or if you're using nix with `nix develop`.

You can build backy with `poetry build` making a wheel and a tar archive
in the `dist` directory, or by running `nix-build`.
in the `dist` directory, or by running `nix build`.

Authors
=======
Expand Down
1 change: 1 addition & 0 deletions changelog.d/20230825_181711_jb_feature_nix_flake.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Add nix flake support
24 changes: 10 additions & 14 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
pkgs ? import <nixpkgs> {},
poetry2nix ? pkgs.poetry2nix,
lzo ? pkgs.lzo,
...
}:
poetry2nix.mkPoetryApplication {
projectDir = ./.;
overrides = poetry2nix.overrides.withDefaults (self: super: {
python-lzo = super.python-lzo.overrideAttrs (old: {
buildInputs = [ lzo ];
});
});
}
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).defaultNix
44 changes: 44 additions & 0 deletions flake.lock

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

70 changes: 70 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};

outputs = { self, nixpkgs, ... }:
let
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
pkgs = forAllSystems (system: nixpkgs.legacyPackages.${system});
poetryOverrides = pkgs: [
# https://github.com/nix-community/poetry2nix/pull/899#issuecomment-1620306977
pkgs.poetry2nix.defaultPoetryOverrides
(self: super: {
python-lzo = super.python-lzo.overrideAttrs (old: {
buildInputs = (old.buildInputs or []) ++ [ pkgs.lzo ];
});
scriv = super.scriv.overrideAttrs (old: {
buildInputs = (old.buildInputs or []) ++ [ super.setuptools ];
});
consulate-fc-nix-test = super.consulate-fc-nix-test.overrideAttrs (old: {
buildInputs = (old.buildInputs or []) ++ [ super.setuptools super.setuptools-scm ];
});
})
];
poetryEnv = pkgs: pkgs.poetry2nix.mkPoetryEnv {
projectDir = self;
python = pkgs.python310;
overrides = poetryOverrides pkgs;
editablePackageSources = {
backy = ./src;
};
};
in
{
packages = forAllSystems (system: {
default = pkgs.${system}.poetry2nix.mkPoetryApplication {
projectDir = self;
doCheck = true;
python = pkgs.${system}.python310;
overrides = poetryOverrides pkgs.${system};
};

venv = poetryEnv pkgs.${system};
});

devShells = forAllSystems (system: {
default = pkgs.${system}.mkShellNoCC {
BACKY_CMD = "backy";
packages = with pkgs.${system}; [
(poetryEnv pkgs.${system})
poetry
];
};
});

checks = forAllSystems (system: {
pytest = pkgs.${system}.runCommand "pytest" {
nativeBuildInputs = [ (poetryEnv pkgs.${system}) ];
} ''
export BACKY_CMD=backy
cd ${self}
pytest -vv -p no:cacheprovider --no-cov
touch $out
'';
});
};
}
27 changes: 10 additions & 17 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
{
pkgs ? import <nixpkgs> {},
...
}:
pkgs.mkShell {
buildInputs = [ pkgs.lzo ];
packages = [ pkgs.poetry ];
shellHook = ''
# check if `poetry env info --path`/bin/activate exists
POETRY_ENV_PATH=$(poetry env info --path)/bin/activate
if [ -f $POETRY_ENV_PATH ]; then
source $POETRY_ENV_PATH
else
echo "Run \`poetry install\` to install dependencies first"
fi
'';
}
(import
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }
).shellNix
9 changes: 6 additions & 3 deletions smoketest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
set -e
umask 022

source `poetry env info --path`/bin/activate
if [[ -z "$BACKY_CMD" ]]; then
echo "error: BACKY_CMD is not set. Set it manually or call via pytest"
exit 2
fi

BACKUP=$(mktemp -d -t backy.test.XXXXX)
BACKY="backy -l ${BACKUP}/backy.log"
BACKY="$BACKY_CMD -l ${BACKUP}/backy.log"
export TZ=Europe/Berlin

mkdir ${BACKUP}/backup
Expand Down Expand Up @@ -40,7 +43,7 @@ $BACKY backup manual:test
echo "Done."

echo -n "Backing up img_state1.img with unknown tag. "
! $BACKY backup unknown
$BACKY backup unknown && exit 1
echo "Done."

echo -n "Restoring img_state1.img from level 0. "
Expand Down
7 changes: 4 additions & 3 deletions src/backy/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

@pytest.fixture(autouse=True, scope="session")
def fix_pytest_coverage_465():
os.environ["COV_CORE_SOURCE"] = os.path.abspath(
os.environ["COV_CORE_SOURCE"]
)
if "COV_CORE_SOURCE" in os.environ:
os.environ["COV_CORE_SOURCE"] = os.path.abspath(
os.environ["COV_CORE_SOURCE"]
)


@pytest.fixture
Expand Down
5 changes: 3 additions & 2 deletions src/backy/tests/test_backy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

import backy.backup
from backy.ext_deps import BASH
from backy.ext_deps import BACKY_CMD, BASH
from backy.tests import Ellipsis


Expand Down Expand Up @@ -107,7 +107,8 @@ def test_smoketest_internal(tmpdir, log):
@pytest.mark.slow
def test_smoketest_external():
output = subprocess.check_output(
[BASH, os.path.dirname(__file__) + "/../../../smoketest.sh"]
[BASH, os.path.dirname(__file__) + "/../../../smoketest.sh"],
env=os.environ | {"BACKY_CMD": BACKY_CMD},
)
output = output.decode("utf-8")
assert (
Expand Down

0 comments on commit 08c74d8

Please sign in to comment.