pds-bridge/flake.nix

128 lines
4 KiB
Nix
Raw Permalink Normal View History

2025-06-24 13:06:47 -07:00
{
description = "Example Rust development environment for Zero to Nix";
# Flake inputs
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
rust-overlay.url = "github:oxalica/rust-overlay"; # A helper for Rust + Nix
};
# Flake outputs
outputs = { self, nixpkgs, rust-overlay }:
let
pdsDirectory = "/home/pan/prog/atproto/authlink";
# Overlays enable you to customize the Nixpkgs attribute set
overlays = [
# Makes a `rust-bin` attribute available in Nixpkgs
(import rust-overlay)
# Provides a `rustToolchain` attribute for Nixpkgs that we can use to
# create a Rust environment
(self: super: {
rustToolchain = super.rust-bin.stable.latest.default;
})
];
# Systems supported
allSystems = [
"x86_64-linux" # 64-bit Intel/AMD Linux
"aarch64-linux" # 64-bit ARM Linux
"x86_64-darwin" # 64-bit Intel macOS
"aarch64-darwin" # 64-bit ARM macOS
];
# Helper to provide system-specific attributes
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
pkgs = import nixpkgs { inherit overlays system; };
});
# Systemd service configuration
createSystemdService = pkgs: pdsDir: pkgs.writeTextFile {
name = "pds.service";
text = ''
[Unit]
Description=Development Environment Service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=${pkgs.pds}/bin/pds
WorkingDirectory=${pdsDir}
EnvironmentFile=${pdsDir}/.env
Environment=PDS_DATA_DIRECTORY=${pdsDir}/.pds-data
Environment=PDS_BLOBSTORE_DISK_LOCATION=${pdsDir}/.pds-data/blocks
[Install]
WantedBy=multi-user.target
'';
};
# Scripts for managing the systemd service
createServiceScripts = pkgs: pdsDir:
let
serviceFile = createSystemdService pkgs pdsDir;
serviceName = "pds";
in {
startScript = pkgs.writeShellScript "start-dev-service" ''
set -e
# Create user systemd directory if it doesn't exist
mkdir -p ~/.config/systemd/user
# Copy service file
cp -f ${serviceFile} ~/.config/systemd/user/${serviceName}.service
# Reload systemd and start service
systemctl --user daemon-reload
systemctl --user start ${serviceName}
systemctl --user enable ${serviceName}
systemctl --user status ${serviceName} --no-pager
'';
stopScript = pkgs.writeShellScript "stop-dev-service" ''
set -e
if systemctl --user is-enabled --quiet ${serviceName}; then
# Stop and disable service
systemctl --user stop ${serviceName} || true
systemctl --user disable ${serviceName} || true
# Remove service file
rm -f ~/.config/systemd/user/${serviceName}.service
# Reload systemd
systemctl --user daemon-reload
fi
'';
};
in
{
# Development environment output
devShells = forAllSystems ({ pkgs }:
let
scripts = createServiceScripts pkgs pdsDirectory;
in {
default = pkgs.mkShell {
# The Nix packages provided in the environment
packages = (with pkgs; [
systemd
# The package provided by our custom overlay. Includes cargo, Clippy, cargo-fmt,
# rustdoc, rustfmt, and other tools.
sqlx-cli
rustToolchain
]) ++ pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]);
# Only run systemd commands on Linux
shellHook = pkgs.lib.optionalString pkgs.stdenv.isLinux ''
# Cleanup
${scripts.stopScript}
# Start the systemd service
${scripts.startScript}
'';
};
});
};
}