Skip to content

Commit

Permalink
uv: init module
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkolenz committed Nov 13, 2024
1 parent 60bb110 commit 7e9487a
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ let
./programs/topgrade.nix
./programs/translate-shell.nix
./programs/urxvt.nix
./programs/uv.nix
./programs/vdirsyncer.nix
./programs/vifm.nix
./programs/vim-vint.nix
Expand Down
74 changes: 74 additions & 0 deletions modules/programs/uv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{ pkgs, config, lib, ... }:

let

inherit (lib) mkEnableOption mkPackageOption mkOption literalExpression;

tomlFormat = pkgs.formats.toml { };
cfg = config.programs.uv;

in {
meta.maintainers = with lib.maintainers; [ mirkolenz ];

options.programs.uv = {
enable = mkEnableOption "uv";

package = mkPackageOption pkgs "uv" { };

settings = mkOption {
type = tomlFormat.type;
default = { };
example = literalExpression ''
{
python-downloads = "never";
python-preference = "only-system";
pip.index-url = "https://test.pypi.org/simple";
}
'';
description = ''
Configuration written to
{file}`$XDG_CONFIG_HOME/uv/uv.toml`.
See <https://docs.astral.sh/uv/configuration/files/>
and <https://docs.astral.sh/uv/reference/settings/>
for more information.
'';
};

enableBashIntegration = (mkEnableOption "uv's bash integration") // {
default = true;
};

enableZshIntegration = (mkEnableOption "uv's zsh integration") // {
default = true;
};

enableFishIntegration = (mkEnableOption "uv's fish integration") // {
default = true;
};
};

config = lib.mkIf cfg.enable {
home.packages = [ cfg.package ];

xdg.configFile."uv/uv.toml" = lib.mkIf (cfg.settings != { }) {
source = tomlFormat.generate "uv-config" cfg.settings;
};

programs.bash.initExtra = lib.mkIf cfg.enableBashIntegration ''
eval "$(${lib.getExe cfg.package} generate-shell-completion bash)"
eval "$(${
lib.getExe' cfg.package "uvx"
} --generate-shell-completion bash)"
'';

programs.zsh.initExtra = lib.mkIf cfg.enableZshIntegration ''
eval "$(${lib.getExe cfg.package} generate-shell-completion zsh)"
eval "$(${lib.getExe' cfg.package "uvx"} --generate-shell-completion zsh)"
'';

programs.fish.interactiveShellInit = lib.mkIf cfg.enableFishIntegration ''
${lib.getExe cfg.package} generate-shell-completion fish | source
${lib.getExe' cfg.package "uvx"} --generate-shell-completion fish | source
'';
};
}
1 change: 1 addition & 0 deletions tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ in import nmtSrc {
./modules/programs/tmux
./modules/programs/topgrade
./modules/programs/translate-shell
./modules/programs/uv
./modules/programs/vifm
./modules/programs/vim-vint
./modules/programs/vscode
Expand Down
23 changes: 23 additions & 0 deletions tests/modules/programs/uv/bash.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{ ... }:

{
programs = {
uv.enable = true;
bash = {
enable = true;
enableCompletion = false;
};
};

test.stubs = { uv = { name = "uv"; }; };

nmt.script = ''
assertFileExists home-files/.bashrc
assertFileContains \
home-files/.bashrc \
'eval "$(@uv@/bin/uv generate-shell-completion bash)"'
assertFileContains \
home-files/.bashrc \
'eval "$(@uv@/bin/uvx --generate-shell-completion bash)"'
'';
}
8 changes: 8 additions & 0 deletions tests/modules/programs/uv/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
uv-bash = ./bash.nix;
uv-example-settings = ./example-settings.nix;
uv-fish = ./fish.nix;
uv-no-settings = ./no-settings.nix;
uv-no-shell = ./no-shell.nix;
uv-zsh = ./zsh.nix;
}
27 changes: 27 additions & 0 deletions tests/modules/programs/uv/example-settings.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{ pkgs, ... }:

{
programs.uv = {
enable = true;
settings = {
python-downloads = "never";
python-preference = "only-system";
pip.index-url = "https://test.pypi.org/simple";
};
};

test.stubs.uv = { };

nmt.script = let
expectedConfigPath = "home-files/.config/uv/uv.toml";
expectedConfigContent = pkgs.writeText "uv.config-custom.expected" ''
python-downloads = "never"
python-preference = "only-system"
[pip]
index-url = "https://test.pypi.org/simple"
'';
in ''
assertFileExists "${expectedConfigPath}"
assertFileContent "${expectedConfigPath}" "${expectedConfigContent}"
'';
}
24 changes: 24 additions & 0 deletions tests/modules/programs/uv/fish.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{ lib, ... }:

{
programs = {
uv.enable = true;
fish.enable = true;
};

# Needed to avoid error with dummy fish package.
xdg.dataFile."fish/home-manager_generated_completions".source =
lib.mkForce (builtins.toFile "empty" "");

test.stubs = { uv = { name = "uv"; }; };

nmt.script = ''
assertFileExists home-files/.config/fish/config.fish
assertFileContains \
home-files/.config/fish/config.fish \
'@uv@/bin/uv generate-shell-completion fish | source'
assertFileContains \
home-files/.config/fish/config.fish \
'@uv@/bin/uvx --generate-shell-completion fish | source'
'';
}
10 changes: 10 additions & 0 deletions tests/modules/programs/uv/no-settings.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ ... }: {
programs.uv = { enable = true; };

test.stubs.uv = { };

nmt.script = let expectedConfigPath = "home-files/.config/uv/uv.toml";
in ''
assertPathNotExists "${expectedConfigPath}"
'';
}
25 changes: 25 additions & 0 deletions tests/modules/programs/uv/no-shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{ lib, ... }:

{
programs = {
uv = {
enable = true;
enableBashIntegration = false;
enableZshIntegration = false;
enableFishIntegration = false;
};
bash.enable = true;
zsh.enable = true;
fish.enable = true;
};

# Needed to avoid error with dummy fish package.
xdg.dataFile."fish/home-manager_generated_completions".source =
lib.mkForce (builtins.toFile "empty" "");

nmt.script = ''
assertFileNotRegex home-files/.zshrc 'uv generate-shell-completion zsh'
assertFileNotRegex home-files/.bashrc 'uv generate-shell-completion bash'
assertFileNotRegex home-files/.config/fish/config.fish 'uv generate-shell-completion fish'
'';
}
20 changes: 20 additions & 0 deletions tests/modules/programs/uv/zsh.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{ ... }:

{
programs = {
uv.enable = true;
zsh.enable = true;
};

test.stubs = { uv = { name = "uv"; }; };

nmt.script = ''
assertFileExists home-files/.zshrc
assertFileContains \
home-files/.zshrc \
'eval "$(@uv@/bin/uv generate-shell-completion zsh)"'
assertFileContains \
home-files/.zshrc \
'eval "$(@uv@/bin/uvx --generate-shell-completion zsh)"'
'';
}

0 comments on commit 7e9487a

Please sign in to comment.