diff --git a/.envrc.example b/.envrc.example new file mode 100644 index 00000000..1d953f4b --- /dev/null +++ b/.envrc.example @@ -0,0 +1 @@ +use nix diff --git a/.gitignore b/.gitignore index f3efc880..21289e8f 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,6 @@ htmlcov/ .coverage .coverage.* .cache + +# nix/direnv +.envrc diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..91c0b063 --- /dev/null +++ b/flake.lock @@ -0,0 +1,247 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "mdbook-nixdoc": { + "inputs": { + "nix-github-actions": [ + "pyproject-nix", + "nix-github-actions" + ], + "nixpkgs": [ + "pyproject-nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1708395692, + "narHash": "sha256-smf0VmxGbjJDZqKvxxG3ZVqubgbVwAWG26wPo+BT/A0=", + "owner": "adisbladis", + "repo": "mdbook-nixdoc", + "rev": "d6a71b114b9221c0b4f20d31b81766d072cc26be", + "type": "github" + }, + "original": { + "owner": "adisbladis", + "repo": "mdbook-nixdoc", + "type": "github" + } + }, + "nix-github-actions": { + "inputs": { + "nixpkgs": [ + "pyproject-nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1703863825, + "narHash": "sha256-rXwqjtwiGKJheXB43ybM8NwWB8rO2dSRrEqes0S7F5Y=", + "owner": "nix-community", + "repo": "nix-github-actions", + "rev": "5163432afc817cf8bd1f031418d1869e4c9d5547", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nix-github-actions", + "type": "github" + } + }, + "nix2containerPkg": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1712990762, + "narHash": "sha256-hO9W3w7NcnYeX8u8cleHiSpK2YJo7ecarFTUlbybl7k=", + "owner": "nlewo", + "repo": "nix2container", + "rev": "20aad300c925639d5d6cbe30013c8357ce9f2a2e", + "type": "github" + }, + "original": { + "owner": "nlewo", + "repo": "nix2container", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1712920918, + "narHash": "sha256-1yxFvUcJfUphK9V91KufIQom7gCsztza0H4Rz2VCWUU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "92323443a56f4e9fc4e4b712e3119f66d0969297", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1701282334, + "narHash": "sha256-MxCVrXY6v4QmfTwIysjjaX0XUhqBbxTWWB4HXtDYsdk=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "057f9aecfb71c4437d2b27d3323df7f93c010b7e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "23.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1709479366, + "narHash": "sha256-n6F0n8UV6lnTZbYPl1A9q1BS0p4hduAv1mGAP17CVd0=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "b8697e57f10292a6165a20f03d2f42920dfaf973", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_4": { + "locked": { + "lastModified": 1708475490, + "narHash": "sha256-g1v0TsWBQPX97ziznfJdWhgMyMGtoBFs102xSYO4syU=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "0e74ca98a74bc7270d28838369593635a5db3260", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "pyproject-nix": { + "inputs": { + "mdbook-nixdoc": "mdbook-nixdoc", + "nix-github-actions": "nix-github-actions", + "nixpkgs": "nixpkgs_3" + }, + "locked": { + "lastModified": 1712595520, + "narHash": "sha256-6F1KAT+iXukGaoBJ0uiAVlPL8VsgKvwtyt/3WASMwiI=", + "owner": "nix-community", + "repo": "pyproject.nix", + "rev": "794220b75a5cddd88f2a6ac6e557a01bc3a9806c", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "pyproject.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nix2containerPkg": "nix2containerPkg", + "nixpkgs": "nixpkgs_2", + "pyproject-nix": "pyproject-nix", + "treefmt-nix": "treefmt-nix" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": "nixpkgs_4" + }, + "locked": { + "lastModified": 1714058656, + "narHash": "sha256-Qv4RBm4LKuO4fNOfx9wl40W2rBbv5u5m+whxRYUMiaA=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "c6aaf729f34a36c445618580a9f95a48f5e4e03f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..04eaee87 --- /dev/null +++ b/flake.nix @@ -0,0 +1,91 @@ +{ + description = "ddapm_test_agent"; + nixConfig.bash-prompt-prefix = "\[ddapm_test_agent\] "; + + inputs = { + flake-utils.url = "github:numtide/flake-utils"; + treefmt-nix.url = "github:numtide/treefmt-nix"; + nixpkgs.url = "github:NixOS/nixpkgs/23.11"; + + pyproject-nix.url = "github:nix-community/pyproject.nix"; + + nix2containerPkg.url = "github:nlewo/nix2container"; + }; + + outputs = { + self, + nixpkgs, + flake-utils, + treefmt-nix, + pyproject-nix, + nix2containerPkg, + }: + flake-utils.lib.eachDefaultSystem (system: let + project = pyproject-nix.lib.project.loadPyproject { + projectRoot = ./.; + }; + pkgs = nixpkgs.legacyPackages.${system}; + nix2container = nix2containerPkg.packages.${system}.nix2container; + + python = pkgs.python312; + pythonPkgs = pkgs.python312Packages; + + packageAttrs = project.renderers.buildPythonPackage {inherit python;}; + packageDeps = project.renderers.withPackages {inherit python;}; + + pythonDevEnv = python.withPackages packageDeps; + + treefmt = treefmt-nix.lib.evalModule pkgs ./treefmt.nix; + + finalPackage = python.pkgs.buildPythonPackage packageAttrs; + finalApp = python.pkgs.buildPythonApplication packageAttrs; + devEnv = pkgs.buildEnv { + name = "root"; + paths = [pkgs.bashInteractive pkgs.coreutils treefmt.config.build.wrapper pythonDevEnv]; + pathsToLink = ["/bin"]; + }; + in { + packages = { + python = python; + ciContainer = nix2container.buildImage { + name = "registry.ddbuild.io/apm-reliability-environment/handmade/nixci"; + + copyToRoot = pkgs.buildEnv { + name = "root"; + paths = [devEnv pythonPkgs.pytest]; + pathsToLink = ["/bin"]; + }; + }; + toolContainer = nix2container.buildImage { + name = "registry.ddbuild.io/apm-reliability-environment/handmade/docker-builder"; + tag = "latest"; + config = { + entrypoint = ["/bin/docker-builder"]; + }; + + copyToRoot = pkgs.buildEnv { + name = "root"; + paths = [finalApp]; + pathsToLink = ["/bin"]; + }; + }; + }; + + packages.default = finalPackage; + formatter = treefmt.config.build.wrapper; + + devShells.default = + pkgs.mkShell + { + venvDir = "./.venv"; + nativeBuildInputs = [pythonDevEnv pythonPkgs.venvShellHook]; + packages = [ + devEnv + pythonPkgs.pytest + ]; + postShellHook = '' + export PYTHONPATH="$PYTHONPATH:$(pwd)" # ensuring pytest invocation works + ''; + }; + }); +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 00000000..8745f502 --- /dev/null +++ b/shell.nix @@ -0,0 +1 @@ +(builtins.getFlake ("git+file://" + toString ./.)).devShells.${builtins.currentSystem}.default diff --git a/test_deps.txt b/test_deps.txt deleted file mode 100644 index bfa92307..00000000 --- a/test_deps.txt +++ /dev/null @@ -1,3 +0,0 @@ -ddtrace==2.6.0 -pytest -riot==0.13.0 diff --git a/treefmt.nix b/treefmt.nix new file mode 100644 index 00000000..ba8eae4c --- /dev/null +++ b/treefmt.nix @@ -0,0 +1,12 @@ +# treefmt.nix +{pkgs, ...}: { + # Used to find the project root + projectRootFile = "flake.nix"; + # Enable the Nix formatter "alejandra" + programs.alejandra.enable = true; + + # Format py sources + programs.black.enable = true; + # Format .sh scripts + programs.shfmt.enable = true; +}