Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions .devops/nix/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
lib,
stdenv,
cmake,
ninja,
pkg-config,
rocmPackages,
cudaPackages,
vulkan-headers,
vulkan-loader,
vulkan-tools,
glslang,
shaderc,
darwin,
python-scripts,
config,
version,

# Overridable feature flags
cudaSupport ? config.cudaSupport or false,
vulkanSupport ? false,
metalSupport ? stdenv.isDarwin,
rocmSupport ? config.rocmSupport or false,
rocmGpuTargets ? (lib.optionals rocmSupport rocmPackages.clr.gpuTargets),
}:

stdenv.mkDerivation (finalAttrs: {
pname = "audio.cpp";
inherit version;
src = lib.cleanSource ../..;

nativeBuildInputs = [
cmake
ninja
pkg-config
]
++ lib.optional cudaSupport cudaPackages.cuda_nvcc
++ lib.optional rocmSupport rocmPackages.clr;

buildInputs = [
python-scripts
]
++ lib.optionals vulkanSupport [
vulkan-headers
vulkan-loader
vulkan-tools
glslang
shaderc
]
++ lib.optionals cudaSupport [
cudaPackages.cudatoolkit
]
++ lib.optionals rocmSupport [
rocmPackages.clr
rocmPackages.hipblas
rocmPackages.rocblas
];

cmakeFlags = [
"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
"-DENGINE_ENABLE_NATIVE_CPU=ON"
"-DENGINE_ENABLE_LLAMAFILE=ON"
] ++ lib.optional stdenv.isDarwin "-DENGINE_ENABLE_OPENMP=OFF"
++ lib.optional vulkanSupport "-DENGINE_ENABLE_VULKAN=ON"
++ lib.optional cudaSupport "-DENGINE_ENABLE_CUDA=ON"
++ lib.optional metalSupport "-DENGINE_ENABLE_METAL=ON"
++ lib.optional rocmSupport "-DENGINE_ENABLE_HIP=ON"
++ lib.optional rocmSupport "-DCMAKE_HIP_COMPILER=${rocmPackages.llvm.clang}/bin/clang"
++ lib.optional rocmSupport "-DGPU_TARGETS=${lib.concatStringsSep ";" rocmGpuTargets}";

env = lib.optionalAttrs rocmSupport {
ROCM_PATH = "${rocmPackages.clr}";
};

installPhase = ''
runHook preInstall

mkdir -p $out/bin

# Copy the built C++ executables directly from the bin directory
cp bin/audiocpp_cli bin/audiocpp_server bin/audiocpp_gguf $out/bin/

# Install the python model manager script
cp $src/tools/model_manager.py $out/bin/audiocpp_model_manager
chmod +x $out/bin/audiocpp_model_manager

# Patch the shebang to use our python environment with torch/safetensors/pyyaml
patchShebangs $out/bin/audiocpp_model_manager

runHook postInstall
'';

meta = with lib; {
description = "A high-performance C++ audio inference framework";
homepage = "https://github.com/0xShug0/audio.cpp";
license = licenses.mit;
platforms = platforms.unix;
mainProgram = "audiocpp_cli";
};
})
11 changes: 11 additions & 0 deletions .devops/nix/python-scripts.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
lib,
python3,
}:

python3.withPackages (ps: with ps; [
ps.torch
ps.safetensors
ps.pyyaml
ps.numpy
])
12 changes: 12 additions & 0 deletions .devops/nix/scope.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
lib,
newScope,
version,
python-scripts,
}:

lib.makeScope newScope (self: {
audiocpp = self.callPackage ./package.nix {
inherit version python-scripts;
};
})
87 changes: 36 additions & 51 deletions docs/build/nixos.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,69 @@
## Prerequisites

- The [Nix package manager](https://nixos.org/download) must be installed.
- [Flakes](https://nixos.wiki/wiki/Flakes) must be enabled in your Nix configuration.
- [Flakes](https://nixos.wiki/wiki/Flakes) must be enabled.

## Packages

The `flake.nix` provides multiple platform and hardware-specific builds.
The following backends are available:
- **`vulkan`**: Hardware-accelerated build using Vulkan. (Default on Linux)
- **`cuda`**: Hardware-accelerated build using NVIDIA CUDA.
- **`metal`**: Hardware-accelerated build using Apple Metal. (Default on macOS)
- **`cpu`**: Portable CPU-only build.
The flake provides backend-specific builds:

All packages include the main tools: ***audiocpp_cli***, ***audiocpp_server***, ***audiocpp_gguf***, and the Python-based ***audiocpp_model_manager***.
| Package | Backend | Platform |
| -------------- | ------------------------ | -------- |
| `cpu` | CPU-only | All |
| `vulkan` | Vulkan | All |
| `cuda` | NVIDIA CUDA | Linux |
| `rocm` | AMD ROCm | Linux |
| `rocm-gfx1151` | AMD ROCm (single target) | Linux |
| `metal` | Apple Metal | macOS |

## Build the package
All packages include: **audiocpp_cli**, **audiocpp_server**, **audiocpp_gguf**, and **audiocpp_model_manager**.

### Default (Auto-detected OS)

Build the default package for your operating system:

```bash
nix build .
```

### Specific Backends

Build explicitly for a desired backend:
## Build

```bash
nix build .#cpu
nix build .#vulkan
nix build .#cuda
nix build .#metal
nix build .#cpu
nix build .#rocm
nix build .#rocm-gfx1151 # Single GPU target (faster)
```

## Usage

After building, the compiled binaries will be available in the `result/bin/` directory.

### Running the CLI

You can execute the CLI directly via `nix run` (which uses `audiocpp_cli` as the main program):
## Run

```bash
nix run .#vulkan -- <arguments...>
nix run .#rocm -- --backend hip --task tts --family supertonic --model ./model --text "hello"
./result/bin/audiocpp_cli --backend hip --device 0
```

### Running the Server
## Download Models

To run the server, use `nix shell` or execute from the build result:
The `python-scripts` package includes all dependencies for `model_manager.py`:

```bash
nix shell .#vulkan -c audiocpp_server <arguments...>
# or
./result/bin/audiocpp_server <arguments...>
nix shell .#python-scripts -c python3 tools/model_manager.py install supertonic_3
```

### Downloading Models (Model Manager)

The flake seamlessly bundles the `model_manager.py` Python script along with all its required dependencies (PyTorch, Safetensors, etc.) as `audiocpp_model_manager`.
## Development Shell

```bash
nix shell .#vulkan -c audiocpp_model_manager install supertonic_3
nix develop # Default backend
nix develop .#cuda # CUDA dev environment
```

## Development Shell
## Custom GPU Target

If you want to work on `audio.cpp` and need a development environment with `cmake`, `ninja`, and all the necessary C++ and Python dependencies pre-configured, simply run:
Override `hipGpuTargets` for a specific GPU:

```bash
nix develop
nix build --impure --expr '(builtins.getFlake (toString ./.)).outputs.packages.x86_64-linux.rocm.override { rocmGpuTargets = ["gfx1151"]; }'
```

## Using in a NixOS Configuration

You can easily include `audio.cpp` as a flake input in your own NixOS configuration or other Nix projects.

In your `flake.nix`, add it to your `inputs`, making sure to use `follows` to avoid duplicating `nixpkgs` versions:
## NixOS Configuration

```nix
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

audiocpp.url = "github:0xShug0/audio.cpp";
audiocpp.inputs.nixpkgs.follows = "nixpkgs";
};
Expand All @@ -94,14 +74,19 @@ In your `flake.nix`, add it to your `inputs`, making sure to use `follows` to av
nixosConfigurations.my-machine = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
({ pkgs, ... }: {
({ ... }: {
environment.systemPackages = [
# Add the default package (auto-detects Metal/Vulkan based on OS)
audiocpp.packages.x86_64-linux.default

# Or explicitly select a backend flavor:
# audiocpp.packages.x86_64-linux.vulkan
# audiocpp.packages.x86_64-linux.cuda
# audiocpp.packages.x86_64-linux.rocm

# Custom GPU target:
# audiocpp.packages.x86_64-linux.rocm.override {
# rocmGpuTargets = [ "gfx1151" ];
# }
];
})
];
Expand Down
Loading
Loading