Skip to content

Commit

Permalink
Merge pull request #1360 from nix-community/statix-deadnix
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored Oct 25, 2023
2 parents 6a45de6 + 9897172 commit 60559cd
Show file tree
Hide file tree
Showing 63 changed files with 339 additions and 310 deletions.
26 changes: 0 additions & 26 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,6 @@ jobs:
- name: Check format
run: nix-shell --packages jq diffutils --run "diff --unified overrides/build-systems.json <(jq --from-file overrides/sort-build-systems.jq --raw-output --sort-keys < overrides/build-systems.json)"

nixpkgs-fmt:
runs-on: ubuntu-latest
steps:
- uses: cachix/install-nix-action@v23
with:
nix_path: nixpkgs=channel:nixos-unstable
- uses: actions/checkout@v4
- name: Check format
run: nix run 'nixpkgs#nixpkgs-fmt' -- --check .

black-fmt:
runs-on: ubuntu-latest
steps:
- uses: cachix/install-nix-action@v23
with:
nix_path: nixpkgs=channel:nixos-unstable
- uses: actions/checkout@v4
- uses: cachix/cachix-action@v12
with:
name: poetry2nix
signingKey: "VhaWuN3IyJVpWg+aZvTocVB+W8ziZKKRGLKR53Pkld3YRZxYOUfXZf0fvqF+LkqVW0eA60trVd5vsqNONpX9Hw=="
- name: Check format
run: nix develop -c black --check .

nix-matrix:
runs-on: ubuntu-latest
outputs:
Expand Down Expand Up @@ -74,8 +50,6 @@ jobs:
collect:
runs-on: ubuntu-latest
needs:
- nixpkgs-fmt
- black-fmt
- nix-build
- sort-build-systems
steps:
Expand Down
78 changes: 36 additions & 42 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ pkgs ? import <nixpkgs> { }
, lib ? pkgs.lib
, poetryLib ? import ./lib.nix { inherit lib pkgs; stdenv = pkgs.stdenv; }
, poetryLib ? import ./lib.nix { inherit lib pkgs; inherit (pkgs) stdenv; }
}:
let
inherit (poetryLib) isCompatible readTOML normalizePackageName normalizePackageSet;
Expand All @@ -11,7 +11,7 @@ let
getLicenseBySpdxId = spdxId: spdxLicenses.${spdxId} or spdxId;

# Experimental withPlugins functionality
toPluginAble = (import ./plugins.nix { inherit pkgs lib; }).toPluginAble;
inherit ((import ./plugins.nix { inherit pkgs lib; })) toPluginAble;

# List of known build systems that are passed through from nixpkgs unmodified
knownBuildSystems = lib.importJSON ./known-build-systems.json;
Expand All @@ -35,19 +35,17 @@ let
compat = isCompatible (poetryLib.getPythonVersion py);
depAttrs = builtins.map (d: lib.toLower d) (builtins.attrNames depSet);
in
(
builtins.map
(
dep:
let
pkg = py.pkgs."${normalizePackageName dep}";
constraints = depSet.${dep}.python or "";
isCompat = compat constraints;
in
if isCompat then pkg else null
)
depAttrs
);
builtins.map
(
dep:
let
pkg = py.pkgs."${normalizePackageName dep}";
constraints = depSet.${dep}.python or "";
isCompat = compat constraints;
in
if isCompat then pkg else null
)
depAttrs;

buildSystemPkgs = poetryLib.getBuildSystemPkgs {
inherit pyProject;
Expand Down Expand Up @@ -156,7 +154,7 @@ lib.makeScope pkgs.newScope (self: {
inherit python scripts;
};

editablePackageSources' = lib.filterAttrs (name: path: path != null) editablePackageSources;
editablePackageSources' = lib.filterAttrs (_name: path: path != null) editablePackageSources;
hasEditable = editablePackageSources' != { };
editablePackage = self.mkPoetryEditablePackage {
inherit pyProject python;
Expand Down Expand Up @@ -205,12 +203,10 @@ lib.makeScope pkgs.newScope (self: {
files = pkgMeta.files or lockFiles.${normalizedName};
pythonPackages = self;

sourceSpec = (
(normalizePackageSet pyProject.tool.poetry.dependencies or { }).${normalizedName}
or (normalizePackageSet pyProject.tool.poetry.dev-dependencies or { }).${normalizedName}
or (normalizePackageSet pyProject.tool.poetry.group.dev.dependencies or { }).${normalizedName} # Poetry 1.2.0+
or { }
);
sourceSpec = (normalizePackageSet pyProject.tool.poetry.dependencies or { }).${normalizedName}
or (normalizePackageSet pyProject.tool.poetry.dev-dependencies or { }).${normalizedName}
or (normalizePackageSet pyProject.tool.poetry.group.dev.dependencies or { }).${normalizedName} # Poetry 1.2.0+
or { };
}
);
}
Expand All @@ -228,7 +224,7 @@ lib.makeScope pkgs.newScope (self: {
(
[
# Remove Python packages aliases with non-normalized names to avoid issues with infinite recursion (issue #750).
(self: super: {
(_self: super: {
# Upstream nixpkgs uses non canonical names
async-generator = super.async-generator or super.async_generator or null;
})
Expand Down Expand Up @@ -257,28 +253,28 @@ lib.makeScope pkgs.newScope (self: {
)

# Fix infinite recursion in a lot of packages because of checkInputs
(self: super: lib.mapAttrs
(name: value: (
(_self: super: lib.mapAttrs
(_name: value: (
if lib.isDerivation value && lib.hasAttr "overridePythonAttrs" value
then value.overridePythonAttrs (_: { doCheck = false; })
else value
))
super)

# Null out any filtered packages, we don't want python.pkgs from nixpkgs
(self: super: builtins.listToAttrs (builtins.map (x: { name = normalizePackageName x.name; value = null; }) incompatible))
(_self: _super: builtins.listToAttrs (builtins.map (x: { name = normalizePackageName x.name; value = null; }) incompatible))
# Create poetry2nix layer
baseOverlay

] ++ # User provided overrides
(if builtins.typeOf overrides == "list" then overrides else [ overrides ])
);
packageOverrides = lib.foldr lib.composeExtensions (self: super: { }) overlays;
packageOverrides = lib.foldr lib.composeExtensions (_self: _super: { }) overlays;
py = python.override { inherit packageOverrides; self = py; };

inputAttrs = mkInputAttrs { inherit py pyProject groups checkGroups extras; attrs = { }; includeBuildSystem = false; };

requiredPythonModules = python.pkgs.requiredPythonModules;
inherit (python.pkgs) requiredPythonModules;
/* Include all the nested dependencies which are required for each package.
This guarantees that using the "poetryPackages" attribute will return
complete list of dependencies for the poetry project to be portable.
Expand All @@ -290,7 +286,7 @@ lib.makeScope pkgs.newScope (self: {
poetryPackages = storePackages
++ lib.optional hasScripts scriptsPackage
++ lib.optional hasEditable editablePackage;
poetryLock = poetryLock;
inherit poetryLock;
inherit pyProject;
};

Expand All @@ -310,7 +306,7 @@ lib.makeScope pkgs.newScope (self: {
, python ? pkgs.python3
, preferWheels ? false
, editablePackageSources ? { }
, extraPackages ? ps: [ ]
, extraPackages ? _ps: [ ]
, groups ? [ "dev" ]
, checkGroups ? [ "dev" ]
, extras ? [ "*" ]
Expand All @@ -322,24 +318,22 @@ lib.makeScope pkgs.newScope (self: {

# Automatically add dependencies with develop = true as editable packages, but only if path dependencies
getEditableDeps = set: lib.mapAttrs
(name: value: projectDir + "/${value.path}")
(lib.filterAttrs (name: dep: dep.develop or false && hasAttr "path" dep) set);
(_name: value: projectDir + "/${value.path}")
(lib.filterAttrs (_name: dep: dep.develop or false && hasAttr "path" dep) set);

excludedEditablePackageNames = builtins.filter
(pkg: editablePackageSources."${pkg}" == null)
(builtins.attrNames editablePackageSources);

allEditablePackageSources = (
(getEditableDeps (pyProject.tool.poetry."dependencies" or { }))
allEditablePackageSources = (getEditableDeps (pyProject.tool.poetry."dependencies" or { }))
// (getEditableDeps (pyProject.tool.poetry."dev-dependencies" or { }))
// (
# Poetry>=1.2.0
if pyProject.tool.poetry.group or { } != { } then
builtins.foldl' (acc: g: acc // getEditableDeps pyProject.tool.poetry.group.${g}.dependencies) { } groups
else { }
)
// editablePackageSources
);
# Poetry>=1.2.0
if pyProject.tool.poetry.group or { } != { } then
builtins.foldl' (acc: g: acc // getEditableDeps pyProject.tool.poetry.group.${g}.dependencies) { } groups
else { }
)
// editablePackageSources;

editablePackageSources' = builtins.removeAttrs
allEditablePackageSources
Expand Down Expand Up @@ -413,7 +407,7 @@ lib.makeScope pkgs.newScope (self: {
];
} // {
pname = normalizePackageName pyProject.tool.poetry.name;
version = pyProject.tool.poetry.version;
inherit (pyProject.tool.poetry) version;

inherit src;

Expand Down
37 changes: 37 additions & 0 deletions dev/treefmt.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ pkgs, ... }: {
# Used to find the project root
projectRootFile = "flake.lock";

settings.formatter = {
nix = {
command = "sh";
options = [
"-eucx"
''
${pkgs.lib.getExe pkgs.deadnix} --edit "$@"
for i in "$@"; do
${pkgs.lib.getExe pkgs.statix} fix "$i"
done
${pkgs.lib.getExe pkgs.nixpkgs-fmt} "$@"
''
"--"
];
includes = [ "*.nix" ];
excludes = [ ];
};

python = {
command = "sh";
options = [
"-eucx"
''
${pkgs.lib.getExe pkgs.python3.pkgs.black} "$@"
''
"--" # this argument is ignored by bash
];
includes = [ "*.py" ];
};
};
}
38 changes: 37 additions & 1 deletion flake.lock

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

16 changes: 14 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/master";

treefmt-nix.url = "github:numtide/treefmt-nix";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";

nix-github-actions = {
url = "github:nix-community/nix-github-actions";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, flake-utils, nix-github-actions }:
outputs = { self, nixpkgs, flake-utils, nix-github-actions, treefmt-nix, systems }:
let
eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system});
treefmtEval = eachSystem (pkgs: treefmt-nix.lib.evalModule pkgs ./dev/treefmt.nix);
in

{
overlay = import ./overlay.nix;

Expand All @@ -32,7 +40,9 @@
let
pkgs = mkPkgs "x86_64-linux";
in
import ./tests { inherit pkgs; };
import ./tests { inherit pkgs; } // {
formatting = treefmtEval.x86_64-linux.config.build.check self;
};

x86_64-darwin =
let
Expand Down Expand Up @@ -84,6 +94,8 @@
poetry2nix = import ./default.nix { inherit pkgs; };
in
rec {
formatter = treefmtEval.${system}.config.build.wrapper;

packages = {
poetry2nix = poetry2nix.cli;
default = poetry2nix.cli;
Expand Down
12 changes: 6 additions & 6 deletions lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ let
# hash: SRI hash
# kind: Language implementation and version tag
predictURLFromPypi = lib.makeOverridable (
{ pname, file, hash, kind }:
{ pname, file, kind }:
"https://files.pythonhosted.org/packages/${kind}/${lib.toLower (builtins.substring 0 1 file)}/${pname}/${file}"
);

Expand All @@ -114,9 +114,9 @@ let
fetchFromPypi = lib.makeOverridable (
{ pname, file, version, hash, kind, curlOpts ? "" }:
let
predictedURL = predictURLFromPypi { inherit pname file hash kind; };
predictedURL = predictURLFromPypi { inherit pname file kind; };
in
(pkgs.stdenvNoCC.mkDerivation {
pkgs.stdenvNoCC.mkDerivation {
name = file;
nativeBuildInputs = [
pkgs.buildPackages.curl
Expand All @@ -141,16 +141,16 @@ let
passthru = {
urls = [ predictedURL ]; # retain compatibility with nixpkgs' fetchurl
};
})
}
);

fetchFromLegacy = lib.makeOverridable (
{ python, pname, url, file, hash }:
let
pathParts =
(builtins.filter
builtins.filter
({ prefix, path }: "NETRC" == prefix)
builtins.nixPath);
builtins.nixPath;
netrc_file = if (pathParts != [ ]) then (builtins.head pathParts).path else "";
in
pkgs.runCommand file
Expand Down
2 changes: 1 addition & 1 deletion overlay.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
final: prev: {
final: _prev: {
poetry2nix = import ./default.nix { pkgs = final; };
}
Loading

0 comments on commit 60559cd

Please sign in to comment.