commit 7183e36b269387546261fb67dc07c983ae71d4c5 Author: Moritz Böhme Date: Sun Jan 14 14:23:18 2024 +0100 feat: initial commit diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1bd505f --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Created by https://www.toptal.com/developers/gitignore/api/direnv +# Edit at https://www.toptal.com/developers/gitignore?templates=direnv + +### direnv ### +.direnv +.envrc + +# End of https://www.toptal.com/developers/gitignore/api/direnv diff --git a/example/default.nix b/example/default.nix new file mode 100644 index 0000000..0b2c904 --- /dev/null +++ b/example/default.nix @@ -0,0 +1,5 @@ +{ pkgs ? import { } }: +let + lib = import ../lib.nix { inherit pkgs; }; +in +lib.genNetrc { projectDir = ./.; } diff --git a/example/poetry.lock b/example/poetry.lock new file mode 100644 index 0000000..f43de03 --- /dev/null +++ b/example/poetry.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +package = [] + +[metadata] +lock-version = "2.0" +python-versions = "^3.11" +content-hash = "8ebb161448cb827b31357cc68b7c8ada8dd31f9185cc957d9ccedf69ecb28673" diff --git a/example/poetry.toml b/example/poetry.toml new file mode 100644 index 0000000..dedc2c3 --- /dev/null +++ b/example/poetry.toml @@ -0,0 +1,7 @@ +[http-basic.foo] +username = "foo" +password = "bar" + +[http-basic.foo2] +username = "foo2" +password = "bar2" diff --git a/example/pyproject.toml b/example/pyproject.toml new file mode 100644 index 0000000..6170e88 --- /dev/null +++ b/example/pyproject.toml @@ -0,0 +1,27 @@ +[tool.poetry] +name = "example" +version = "0.1.0" +description = "" +authors = ["Your Name "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.11" + +[[tool.poetry.source]] +name = "foo" +url = "https://foo.bar/simple/" +priority = "primary" + +[[tool.poetry.source]] +name = "foo2" +url = "https://foo2.bar/simple/" +priority = "primary" + +[[tool.poetry.source]] +name = "PyPI" +priority = "primary" + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..dd027e3 --- /dev/null +++ b/flake.lock @@ -0,0 +1,63 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1704982712, + "narHash": "sha256-2Ptt+9h8dczgle2Oo6z5ni5rt/uLMG47UFTR1ry/wgg=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "07f6395285469419cf9d078f59b5b49993198c00", + "type": "github" + }, + "original": { + "id": "flake-parts", + "type": "indirect" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1705133751, + "narHash": "sha256-rCIsyE80jgiOU78gCWN3A0wE0tR2GI5nH6MlS+HaaSQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "9b19f5e77dd906cb52dade0b7bd280339d2a1f3d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "dir": "lib", + "lastModified": 1703961334, + "narHash": "sha256-M1mV/Cq+pgjk0rt6VxoyyD+O8cOUiai8t9Q6Yyq4noY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "b0d36bd0a420ecee3bc916c91886caca87c894e9", + "type": "github" + }, + "original": { + "dir": "lib", + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..929e34f --- /dev/null +++ b/flake.nix @@ -0,0 +1,33 @@ +{ + description = "Description for the project"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + }; + + outputs = inputs@{ flake-parts, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + imports = [ + # To import a flake module + # 1. Add foo to inputs + # 2. Add foo as a parameter to the outputs function + # 3. Add here: foo.flakeModule + + ]; + systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ]; + perSystem = { config, self', inputs', pkgs, system, ... }: { + # Per-system attributes can be defined here. The self' and inputs' + # module parameters provide easy access to attributes of the same + # system. + + # Equivalent to inputs'.nixpkgs.legacyPackages.hello; + packages.default = pkgs.hello; + }; + flake = { + # The usual flake attributes can be defined here, including system- + # agnostic ones like nixosModule and system-enumerating ones, although + # those are more easily expressed in perSystem. + + }; + }; +} diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..33707ad --- /dev/null +++ b/lib.nix @@ -0,0 +1,35 @@ +{ pkgs ? import { } +, lib ? pkgs.lib +}: +{ + genNetrc = + { projectDir ? null + , poetry ? projectDir + "/poetry.toml" + , pyproject ? projectDir + "/pyproject.toml" + }: + let + poetryToml = builtins.fromTOML (builtins.readFile poetry); + pyprojectToml = builtins.fromTOML (builtins.readFile pyproject); + + sourceNames = builtins.attrNames poetryToml.http-basic; + authSources = + let + sourceNeedsAuth = { name, ... }: builtins.elem name sourceNames; + in + builtins.filter sourceNeedsAuth pyprojectToml.tool.poetry.source; + mergedSources = map (source: source // (builtins.getAttr source.name poetryToml.http-basic)) authSources; + + mkMachine = url: + let + matches = builtins.split "(https?://)([^/]*)(.*)" url; + mainMatch = builtins.elemAt matches 1; + domain = builtins.elemAt mainMatch 1; + in + domain; + mkEntry = { url, username, password, ... }: + "machine ${mkMachine url} login ${username} password ${password}"; + entries = map mkEntry mergedSources; + contents = lib.concatLines entries; + in + pkgs.writeText "netrc" contents; +}