Skip to content

Commit

Permalink
feat: Add nix support
Browse files Browse the repository at this point in the history
Build `audible-cli` via nix _including_ all plugins!
Added necessary code to create an AppImage for Linux + docker images via
nix.
Also add GitHub action logic to check whether the package continues to
build via nix, including the AppImage and docker image.
Currently limiting support to Linux x86, as this is the only local
machine.
  • Loading branch information
kai-tub committed Jun 5, 2024
1 parent b3adb9a commit dd493ff
Show file tree
Hide file tree
Showing 7 changed files with 453 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Nix-Test

on:
- push
- pull_request

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- name: Checkout
uses: actions/checkout@v4
- name: Run `nix fmt`
run: nix fmt -- --check *
- name: Run `flake checks`
run: nix flake check -L
- name: Create AppImage
run: nix build .#audible-cli-AppImage
- name: Test appimage
run: ./result decrypt --help
- name: Rename AppImage
if: startsWith(github.ref, 'refs/tags/')
run: cp ./result audible-cli.AppImage
- name: Release-AppImage
uses: softprops/action-gh-release@v2
if: startsWith(github.ref, 'refs/tags/')
with:
files: audible-cli.AppImage
# FUTURE: Push the docker container to an image registry
# see: https://github.com/containers/skopeo
# and usage in: https://github.com/seanrmurphy/nix-container-build-gha/blob/main/scripts/build_and_push_image.sh
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
/src/audible_cli/*.bak
library.csv

result
*.AppImage

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
175 changes: 175 additions & 0 deletions flake.lock

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

49 changes: 49 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
description = "Nix related tooling for a command line interface for audible. With the CLI you can download your Audible books, cover, chapter files & conver them.";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
systems.url = "github:nix-systems/x86_64-linux";
flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz";
nix-filter.url = "github:numtide/nix-filter";
nix-appimage = {
url = "github:ralismark/nix-appimage";
};
};
outputs = {
self,
nixpkgs,
systems,
nix-appimage,
flake-compat,
nix-filter,
} @ inputs: let
eachSystem = nixpkgs.lib.genAttrs (import systems);
pkgsFor = eachSystem (system: (nixpkgs.legacyPackages.${system}.extend self.overlays.default));
in {
formatter = eachSystem (system: pkgsFor.${system}.alejandra);
checks = eachSystem (system: self.packages.${system});
overlays = import ./nix/overlays.nix {
inherit inputs;
lib = nixpkgs.lib; # TODO: Understand this construct
};

packages = eachSystem (system: let
pkgs = pkgsFor.${system};
in rec {
audible-cli = pkgs.audible-cli;
audible-cli-full = pkgs.audible-cli-full;
isbntools = pkgs.python3Packages.isbntools;
audible-cli-AppImage = inputs.nix-appimage.mkappimage.${system} {
drv = audible-cli-full;
name = audible-cli-full.name;
entrypoint = pkgs.lib.getExe audible-cli-full;
};
audible-cli-docker = pkgs.dockerTools.buildLayeredImage {
name = audible-cli-full.pname; # `.name` has illegal docker tag format
tag = "latest";
contents = audible-cli-full;
};
default = audible-cli-full;
});
};
}
115 changes: 115 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
lib,
nix-filter,
python3Packages,
ffmpeg_7-headless,
enable-plugin-decrypt ? true,
enable-plugin-goodreads-transform ? true,
enable-plugin-annotations ? true,
enable-plugin-image-urls ? true,
enable-plugin-listening-stats ? true,
version ? "git",
}:
# The core of the code was taken from nixpkgs and with special thanks to the
# upstream maintainer `jvanbruegge`
# https://github.com/NixOS/nixpkgs/blob/63c3a29ca82437c87573e4c6919b09a24ea61b0f/pkgs/by-name/au/audible-cli/package.nix
python3Packages.buildPythonApplication {
pname = "audible-cli";
inherit version;
pyproject = true;

src = nix-filter {
root = ./..;
include =
[
# Include the "src" path relative to the root
"src"
"LICENSE"
"setup.cfg"
"README.md" # Required by `setup.cfg`
"setup.py"
"nix"
]
++ lib.optionals enable-plugin-annotations [
"plugin_cmds/cmd_get-annotations.py"
]
++ lib.optionals enable-plugin-goodreads-transform [
"plugin_cmds/cmd_goodreads-transform.py"
]
++ lib.optionals enable-plugin-image-urls [
"plugin_cmds/cmd_image-urls.py"
]
++ lib.optionals enable-plugin-listening-stats [
"plugin_cmds/cmd_listening-stats.py"
]
++ lib.optionals enable-plugin-decrypt [
"plugin_cmds/cmd_decrypt.py"
];
};

# there is no real benefit of trying to make ffmpeg smaller, as headless
# only takes about 25MB, whereas Python takes >120MB.
dependencies = lib.optionals enable-plugin-decrypt [ffmpeg_7-headless];
makeWrapperArgs =
lib.optionals
(enable-plugin-annotations || enable-plugin-goodreads-transform || enable-plugin-image-urls || enable-plugin-listening-stats || enable-plugin-decrypt)
["--set AUDIBLE_PLUGIN_DIR $src/plugin_cmds"];

nativeBuildInputs = with python3Packages; [
pythonRelaxDepsHook
setuptools
];
# FUTURE: Renable once shell completion is fixed!
# ++ [
# installShellFiles
# ];

propagatedBuildInputs =
(with python3Packages; [
aiofiles
audible
click
httpx
packaging
pillow
questionary
setuptools
tabulate
toml
tqdm
])
++ lib.optionals enable-plugin-goodreads-transform [
python3Packages.isbntools
];

pythonRelaxDeps = [
"httpx"
];

# FUTURE: Fix fish code_completions & re-enable them
# postInstall = ''
# export PATH=$out/bin:$PATH
# installShellCompletion --cmd audible \
# --bash <(source utils/code_completion/audible-complete-bash.sh) \
# --fish <(source utils/code_completion/audible-complete-zsh-fish.sh) \
# --zsh <(source utils/code_completion/audible-complete-zsh-fish.sh)
# '';

# upstream has no tests
doCheck = false;
# FUTURE: Add import tests for the different plugins!

pythonImportsCheck = [
"audible_cli"
];

# passthru.updateScript = pkgs.nix-update-script {};

meta = {
description = "A command line interface for audible package. With the cli you can download your Audible books, cover, chapter files";
license = lib.licenses.agpl3Only;
homepage = "https://github.com/mkb79/audible-cli";
maintainers = with lib.maintainers; [kai-tub];
mainProgram = "audible";
};
}
Loading

0 comments on commit dd493ff

Please sign in to comment.