From 670d9ecc3e46a6e3265c203c2d136031a3d3548e Mon Sep 17 00:00:00 2001 From: Mirko Lenz Date: Sat, 24 Feb 2024 17:39:35 +0100 Subject: [PATCH] poetry: add module --- modules/misc/news.nix | 10 ++++ modules/modules.nix | 1 + modules/programs/poetry.nix | 55 +++++++++++++++++++ tests/default.nix | 1 + .../programs/poetry/custom-settings.nix | 27 +++++++++ .../programs/poetry/default-settings.nix | 15 +++++ tests/modules/programs/poetry/default.nix | 4 ++ 7 files changed, 113 insertions(+) create mode 100644 modules/programs/poetry.nix create mode 100644 tests/modules/programs/poetry/custom-settings.nix create mode 100644 tests/modules/programs/poetry/default-settings.nix create mode 100644 tests/modules/programs/poetry/default.nix diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 8d7e17a5..bbbf3b8f 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -1506,6 +1506,16 @@ in { A new module is available: 'services.remmina'. ''; } + + { + time = "2024-04-21T20:53:09+00:00"; + message = '' + A new module is available: 'programs.poetry'. + + Poetry is a tool that helps you manage Python project dependencies and + packages. See https://python-poetry.org/ for more. + ''; + } ]; }; } diff --git a/modules/modules.nix b/modules/modules.nix index 154f2bd9..01247077 100644 --- a/modules/modules.nix +++ b/modules/modules.nix @@ -186,6 +186,7 @@ let ./programs/pistol.nix ./programs/piston-cli.nix ./programs/pls.nix + ./programs/poetry.nix ./programs/powerline-go.nix ./programs/pqiv.nix ./programs/pubs.nix diff --git a/modules/programs/poetry.nix b/modules/programs/poetry.nix new file mode 100644 index 00000000..d297f0c7 --- /dev/null +++ b/modules/programs/poetry.nix @@ -0,0 +1,55 @@ +{ pkgs, config, lib, ... }: + +let + + inherit (lib) mkEnableOption mkPackageOption mkOption literalExpression; + + tomlFormat = pkgs.formats.toml { }; + + configDir = if pkgs.stdenv.isDarwin then + "Library/Application Support" + else + config.xdg.configHome; + + cfg = config.programs.poetry; + +in { + meta.maintainers = with lib.maintainers; [ mirkolenz ]; + + options.programs.poetry = { + enable = mkEnableOption "poetry"; + + package = mkPackageOption pkgs "poetry" { + example = "pkgs.poetry.withPlugins (ps: with ps; [ poetry-plugin-up ])"; + extraDescription = "May be used to install custom poetry plugins."; + }; + + settings = mkOption { + type = tomlFormat.type; + default = { }; + example = literalExpression '' + { + virtualenvs.create = true; + virtualenvs.in-project = true; + } + ''; + description = '' + Configuration written to + {file}`$XDG_CONFIG_HOME/pypoetry/config.toml` on Linux or + {file}`$HOME/Library/Application Support/pypoetry/config.toml` on Darwin. + See + + for more information. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + home.packages = [ cfg.package ]; + + home.file."${configDir}/pypoetry/config.toml" = + lib.mkIf (cfg.settings != { }) { + source = tomlFormat.generate "poetry-config" cfg.settings; + }; + }; +} diff --git a/tests/default.nix b/tests/default.nix index 3729ce98..4b7b6ebf 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -122,6 +122,7 @@ in import nmtSrc { ./modules/programs/pet ./modules/programs/pistol ./modules/programs/pls + ./modules/programs/poetry ./modules/programs/powerline-go ./modules/programs/pubs ./modules/programs/pyenv diff --git a/tests/modules/programs/poetry/custom-settings.nix b/tests/modules/programs/poetry/custom-settings.nix new file mode 100644 index 00000000..fc979306 --- /dev/null +++ b/tests/modules/programs/poetry/custom-settings.nix @@ -0,0 +1,27 @@ +{ pkgs, ... }: + +{ + programs.poetry = { + enable = true; + settings = { + virtualenvs.create = true; + virtualenvs.in-project = true; + }; + }; + + test.stubs.poetry = { }; + + nmt.script = let + expectedConfDir = + if pkgs.stdenv.isDarwin then "Library/Application Support" else ".config"; + expectedConfigPath = "home-files/${expectedConfDir}/pypoetry/config.toml"; + expectedConfigContent = pkgs.writeText "poetry.config-custom.expected" '' + [virtualenvs] + create = true + in-project = true + ''; + in '' + assertFileExists "${expectedConfigPath}" + assertFileContent "${expectedConfigPath}" "${expectedConfigContent}" + ''; +} diff --git a/tests/modules/programs/poetry/default-settings.nix b/tests/modules/programs/poetry/default-settings.nix new file mode 100644 index 00000000..145cf18a --- /dev/null +++ b/tests/modules/programs/poetry/default-settings.nix @@ -0,0 +1,15 @@ +{ pkgs, ... }: + +{ + programs.poetry = { enable = true; }; + + test.stubs.poetry = { }; + + nmt.script = let + expectedConfDir = + if pkgs.stdenv.isDarwin then "Library/Application Support" else ".config"; + expectedConfigPath = "home-files/${expectedConfDir}/pypoetry/config.toml"; + in '' + assertPathNotExists "${expectedConfigPath}" + ''; +} diff --git a/tests/modules/programs/poetry/default.nix b/tests/modules/programs/poetry/default.nix new file mode 100644 index 00000000..50af2a33 --- /dev/null +++ b/tests/modules/programs/poetry/default.nix @@ -0,0 +1,4 @@ +{ + poetry-default-settings = ./default-settings.nix; + poetry-custom-settings = ./custom-settings.nix; +}