Skip to content

Commit

Permalink
Pin nixpkgs (#88)
Browse files Browse the repository at this point in the history
* Pin nixpkgs according to nix-index-database

This PR should fix #87 by ensuring that what database the
nix-index-database is pointing up is the nixpkgs version we will end up
using in the default.nix files.

For now this only changes the non-Flake version. The Flake ones will
come in another PR.

* Implement nixpkgs pining in Flake versions

* Fix checks

* Bump UUID to regenerate all nix-alien files

Since there is so many changes in nix-alien right now, this commit will
bump the UUID used as a base for hashing the names, so we can ensure
everyone will use the new changes.

* Simplify substituteInPlace calls
  • Loading branch information
thiagokokada authored Oct 24, 2023
1 parent 0d4ef3d commit a72ce7c
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 64 deletions.
26 changes: 18 additions & 8 deletions checks.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
{ pkgs ? (import ./compat.nix).pkgs
, self ? (import ./compat.nix).flake
}:

let
rev = "0.1.0+ci";
in
{
nix-alien-ci = pkgs.callPackage ./nix-alien.nix {
inherit rev;
ci = true;
};
inherit (self.outputs.packages.${pkgs.system}) nix-alien nix-index-update;

nix-index-update-ci = pkgs.callPackage ./nix-index-update.nix { };
check-py-files = pkgs.runCommand "check-py-files"
{
src = ./.;
nativeBuildInputs = with pkgs; [
python3.pkgs.black
python3.pkgs.mypy
ruff
];
} ''
touch $out
export PYLINTHOME="$(mktemp -d)"
export RUFF_CACHE_DIR="$(mktemp -d)"
black --check $src/nix_alien
mypy --ignore-missing-imports $src/nix_alien
ruff check $src/nix_alien
'';

check-nix-files = pkgs.runCommand "check-nix-files"
{
Expand Down
1 change: 1 addition & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ in
nix-alien = pkgs.callPackage ./nix-alien.nix {
inherit rev;
nix-index = self.inputs.nix-index-database.packages.${pkgs.system}.nix-index-with-db;
nixpkgs-src = self.inputs.nix-index-database.inputs.nixpkgs.sourceInfo;
};

# For backwards compat, may be removed in the future
Expand Down
22 changes: 18 additions & 4 deletions flake.lock

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

9 changes: 3 additions & 6 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
};
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
nix-index-database = {
url = "github:Mic92/nix-index-database";
inputs.nixpkgs.follows = "nixpkgs";
};
nix-index-database.url = "github:Mic92/nix-index-database";
};

outputs = { self, nixpkgs, flake-utils, ... }:
Expand All @@ -32,7 +29,7 @@
default = self.outputs.packages.${system}.nix-alien;
};

checks = import ./checks.nix { inherit pkgs; };
checks = import ./checks.nix { inherit pkgs self; };

apps =
let
Expand All @@ -46,6 +43,6 @@
nix-index-update = mkApp { drv = self.outputs.packages.${system}.nix-index-update; };
};

devShells.default = import ./shell.nix { inherit pkgs; };
devShells.default = import ./shell.nix { inherit pkgs self; };
}));
}
23 changes: 9 additions & 14 deletions nix-alien.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{ lib
, fzf
, nix-index
, nixpkgs-src ? {
lastModifiedDate = "19700101000000";
rev = "nixpkgs-unstable";
narHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
}
, python3
, ruff
, rev ? null
, dev ? false
, ci ? false
}:

assert dev -> !ci;
let
version = if (rev != null) then rev else "dev";
deps = (lib.importTOML ./pyproject.toml).project.dependencies;
Expand All @@ -29,25 +31,18 @@ python3.pkgs.buildPythonApplication {

preBuild = lib.optionalString (rev != null) ''
echo "__version__ = \"${rev}\"" > nix_alien/_version.py
substituteInPlace {nix_alien,tests}/*.{py,nix} \
--subst-var-by nixpkgsLastModifiedDate ${nixpkgs-src.lastModifiedDate} \
--subst-var-by nixpkgsRev ${nixpkgs-src.rev} \
--subst-var-by nixpkgsHash ${nixpkgs-src.narHash}
'';

doCheck = !dev;

nativeCheckInputs = with python3.pkgs; [
pytestCheckHook
] ++ lib.optionals ci [
black
mypy
ruff
];

preCheck = lib.optionalString ci ''
export PYLINTHOME="$(mktemp -d)"
black --check ./nix_alien
mypy --ignore-missing-imports ./nix_alien
ruff check ./nix_alien
'';

meta = with lib; {
description = "Run unpatched binaries on Nix/NixOS";
homepage = "https://github.com/thiagokokada/nix-alien";
Expand Down
9 changes: 8 additions & 1 deletion nix_alien/fhs_env.template.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{ pkgs ? import <nixpkgs> { } }:
{ pkgs ? import
(builtins.fetchTarball {
name = "nixpkgs-unstable-@nixpkgsLastModifiedDate@";
url = "https://github.com/NixOS/nixpkgs/archive/@[email protected]";
sha256 = "@nixpkgsHash@";
})
{ }
}:

let
inherit (pkgs) buildFHSUserEnv;
Expand Down
4 changes: 2 additions & 2 deletions nix_alien/fhs_env_flake.template.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
description = "${__name__}-fhs";

inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/@nixpkgsRev@";

outputs = { self, nixpkgs }:
let
Expand All @@ -23,7 +23,7 @@

defaultApp.${system} = {
type = "app";
program = "${defaultPackage.${system}}/bin/${__name__}-fhs";
program = "${self.outputs.defaultPackage.${system}}/bin/${__name__}-fhs";
};
};
}
2 changes: 1 addition & 1 deletion nix_alien/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from string import Template
from typing import Callable, Optional

UUID_NAMESPACE = uuid.UUID("f318d4a6-dd46-47ce-995d-e95c17cadcc0")
UUID_NAMESPACE = uuid.UUID("eebf3397-2041-4370-bf33-937b33d5c959")


def edit_file(file: Path) -> subprocess.CompletedProcess:
Expand Down
9 changes: 8 additions & 1 deletion nix_alien/nix_ld.template.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{ pkgs ? import <nixpkgs> { } }:
{ pkgs ? import
(builtins.fetchTarball {
name = "nixpkgs-unstable-@nixpkgsLastModifiedDate@";
url = "https://github.com/NixOS/nixpkgs/archive/@[email protected]";
sha256 = "@nixpkgsHash@";
})
{ }
}:

let
inherit (pkgs) lib stdenv;
Expand Down
5 changes: 2 additions & 3 deletions nix_alien/nix_ld_flake.template.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
description = "${__name__}-nix-ld";

inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/@nixpkgsRev@";

outputs = { self, nixpkgs }:
let
Expand All @@ -15,11 +15,10 @@
NIX_LD_LIBRARY_PATH = with pkgs; lib.makeLibraryPath [
${__packages__}
];
NIX_LD = lib.fileContents "${stdenv.cc}/nix-support/dynamic-linker";
in
pkgs.writeShellScriptBin "${__name__}" ''
export NIX_LD_LIBRARY_PATH='${NIX_LD_LIBRARY_PATH}'${"\${NIX_LD_LIBRARY_PATH:+':'}$NIX_LD_LIBRARY_PATH"}
export NIX_LD='${NIX_LD}'
export NIX_LD="$(cat ${stdenv.cc}/nix-support/dynamic-linker)"
${__program__} "$@"
'';

Expand Down
10 changes: 8 additions & 2 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{ pkgs ? (import ./compat.nix).pkgs }:
{ pkgs ? (import ./compat.nix).pkgs
, self ? (import ./compat.nix).flake
}:

let
inherit (pkgs) python3 callPackage;
nix-alien = python3.pkgs.toPythonModule (callPackage ./nix-alien.nix { dev = true; });
nix-alien = python3.pkgs.toPythonModule (callPackage ./nix-alien.nix {
dev = true;
nix-index = self.inputs.nix-index-database.packages.${pkgs.system}.nix-index-with-db;
nixpkgs-src = self.inputs.nix-index-database.inputs.nixpkgs.sourceInfo;
});
python-with-packages = python3.withPackages (ps: with ps; [
black
mypy
Expand Down
26 changes: 20 additions & 6 deletions tests/test_fhs_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ def test_create_fhs_env_drv(mock_find_libs, pytestconfig):
assert (
fhs_env.create_fhs_env_drv("xyz", additional_packages=["libGL"])
== """\
{ pkgs ? import <nixpkgs> { } }:
{ pkgs ? import
(builtins.fetchTarball {
name = "nixpkgs-unstable-@nixpkgsLastModifiedDate@";
url = "https://github.com/NixOS/nixpkgs/archive/@[email protected]";
sha256 = "@nixpkgsHash@";
})
{ }
}:
let
inherit (pkgs) buildFHSUserEnv;
Expand Down Expand Up @@ -45,7 +52,14 @@ def test_create_fhs_env_drv_with_spaces(mock_find_libs, pytestconfig):
assert (
fhs_env.create_fhs_env_drv("x y z", additional_packages=["libGL"])
== """\
{ pkgs ? import <nixpkgs> { } }:
{ pkgs ? import
(builtins.fetchTarball {
name = "nixpkgs-unstable-@nixpkgsLastModifiedDate@";
url = "https://github.com/NixOS/nixpkgs/archive/@[email protected]";
sha256 = "@nixpkgsHash@";
})
{ }
}:
let
inherit (pkgs) buildFHSUserEnv;
Expand Down Expand Up @@ -81,7 +95,7 @@ def test_create_fhs_env_drv_flake(mock_machine, mock_find_libs, pytestconfig):
{
description = "xyz-fhs";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/@nixpkgsRev@";
outputs = { self, nixpkgs }:
let
Expand All @@ -106,7 +120,7 @@ def test_create_fhs_env_drv_flake(mock_machine, mock_find_libs, pytestconfig):
defaultApp.${system} = {
type = "app";
program = "${defaultPackage.${system}}/bin/xyz-fhs";
program = "${self.outputs.defaultPackage.${system}}/bin/xyz-fhs";
};
};
}
Expand Down Expand Up @@ -222,7 +236,7 @@ def test_main_with_print(monkeypatch, capsys):
assert (
out
== """\
/home/nameless-shelter/.cache/nix-alien/b5ae45f6-276c-53a3-93ab-4a44f35976a4/fhs-env/default.nix
/home/nameless-shelter/.cache/nix-alien/d51a223b-43f0-56c6-b5c8-2404823026ac/fhs-env/default.nix
"""
)

Expand All @@ -232,6 +246,6 @@ def test_main_with_print(monkeypatch, capsys):
assert (
out
== """\
/home/nameless-shelter/.cache/nix-alien/b5ae45f6-276c-53a3-93ab-4a44f35976a4/fhs-env/flake.nix
/home/nameless-shelter/.cache/nix-alien/d51a223b-43f0-56c6-b5c8-2404823026ac/fhs-env/flake.nix
"""
)
18 changes: 9 additions & 9 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ def test_edit_file(monkeypatch):
def test_get_hash_for_program(monkeypatch):
monkeypatch.setenv("HOME", "/home/nameless-shelter")
assert helpers.get_hash_for_program("/home/nameless-shelter/abc") == UUID(
"d0efaea7-176f-56e6-99a0-ea49599a163b"
"0acc4435-07de-59bf-a1c3-a42f55aaca35"
)
assert helpers.get_hash_for_program("~/abc") == UUID(
"d0efaea7-176f-56e6-99a0-ea49599a163b"
"0acc4435-07de-59bf-a1c3-a42f55aaca35"
)
assert helpers.get_hash_for_program("/abc") == UUID(
"f52177f5-def5-5d9e-91fc-ef1283fc54b1"
"cf2f97e0-6eec-5407-aea0-bbecc488d451"
)
assert helpers.get_hash_for_program("/./abc") == UUID(
"f52177f5-def5-5d9e-91fc-ef1283fc54b1"
"cf2f97e0-6eec-5407-aea0-bbecc488d451"
)
assert helpers.get_hash_for_program("/xyz/../abc") == UUID(
"f52177f5-def5-5d9e-91fc-ef1283fc54b1"
"cf2f97e0-6eec-5407-aea0-bbecc488d451"
)


def test_get_cache_path(monkeypatch):
monkeypatch.setenv("HOME", "/home/nameless-shelter")
assert helpers.get_cache_path("/abc") == Path(
"/home/nameless-shelter/.cache/nix-alien/f52177f5-def5-5d9e-91fc-ef1283fc54b1"
"/home/nameless-shelter/.cache/nix-alien/cf2f97e0-6eec-5407-aea0-bbecc488d451"
)

monkeypatch.setenv("XDG_CACHE_HOME", "/")
assert helpers.get_cache_path("/abc") == Path(
"/nix-alien/f52177f5-def5-5d9e-91fc-ef1283fc54b1"
"/nix-alien/cf2f97e0-6eec-5407-aea0-bbecc488d451"
)


Expand All @@ -48,7 +48,7 @@ def test_get_dest_path(monkeypatch):
directory="bar",
filename="foo.nix",
) == Path(
"/home/nameless-shelter/.cache/nix-alien/f52177f5-def5-5d9e-91fc-ef1283fc54b1/bar/foo.nix"
"/home/nameless-shelter/.cache/nix-alien/cf2f97e0-6eec-5407-aea0-bbecc488d451/bar/foo.nix"
)

monkeypatch.setenv("XDG_CACHE_HOME", "/")
Expand All @@ -57,7 +57,7 @@ def test_get_dest_path(monkeypatch):
program="/abc",
directory="bar",
filename="foo.nix",
) == Path("/nix-alien/f52177f5-def5-5d9e-91fc-ef1283fc54b1/bar/foo.nix")
) == Path("/nix-alien/cf2f97e0-6eec-5407-aea0-bbecc488d451/bar/foo.nix")

assert helpers.get_dest_path(
destination="/quux",
Expand Down
Loading

0 comments on commit a72ce7c

Please sign in to comment.