Nix, update nixpkgs and add systemd service support

This commit is contained in:
Julia Lange 2025-08-28 10:50:18 -07:00
parent 6d45fb2f70
commit 8957fe74b1
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
2 changed files with 96 additions and 23 deletions

24
flake.lock generated
View file

@ -2,16 +2,18 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1735563628, "lastModified": 1752436162,
"narHash": "sha256-OnSAY7XDSx7CtDoqNh8jwVwh4xNL/2HaJxGjryLWzX8=", "narHash": "sha256-Kt1UIPi7kZqkSc5HVj6UY5YLHHEzPBkgpNUByuyxtlw=",
"rev": "b134951a4c9f3c995fd7be05f3243f8ecd65d798", "owner": "NixOS",
"revCount": 637546, "repo": "nixpkgs",
"type": "tarball", "rev": "dfcd5b901dbab46c9c6e80b265648481aafb01f8",
"url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.2405.637546%2Brev-b134951a4c9f3c995fd7be05f3243f8ecd65d798/01941dc2-2ab2-7453-8ebd-88712e28efae/source.tar.gz" "type": "github"
}, },
"original": { "original": {
"type": "tarball", "owner": "NixOS",
"url": "https://flakehub.com/f/NixOS/nixpkgs/0.2405.%2A.tar.gz" "ref": "nixos-25.05",
"repo": "nixpkgs",
"type": "github"
} }
}, },
"nixpkgs_2": { "nixpkgs_2": {
@ -41,11 +43,11 @@
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1749695868, "lastModified": 1752547600,
"narHash": "sha256-debjTLOyqqsYOUuUGQsAHskFXH5+Kx2t3dOo/FCoNRA=", "narHash": "sha256-0vUE42ji4mcCvQO8CI0Oy8LmC6u2G4qpYldZbZ26MLc=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "55f914d5228b5c8120e9e0f9698ed5b7214d09cd", "rev": "9127ca1f5a785b23a2fc1c74551a27d3e8b9a28b",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -3,13 +3,15 @@
# Flake inputs # Flake inputs
inputs = { inputs = {
nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2405.*.tar.gz"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
rust-overlay.url = "github:oxalica/rust-overlay"; # A helper for Rust + Nix rust-overlay.url = "github:oxalica/rust-overlay"; # A helper for Rust + Nix
}; };
# Flake outputs # Flake outputs
outputs = { self, nixpkgs, rust-overlay }: outputs = { self, nixpkgs, rust-overlay }:
let let
pdsDirectory = "/home/pan/prog/atproto/appview";
# Overlays enable you to customize the Nixpkgs attribute set # Overlays enable you to customize the Nixpkgs attribute set
overlays = [ overlays = [
# Makes a `rust-bin` attribute available in Nixpkgs # Makes a `rust-bin` attribute available in Nixpkgs
@ -33,10 +35,71 @@
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
pkgs = import nixpkgs { inherit overlays system; }; 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
'';
};
# 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 in
{ {
# Development environment output # Development environment output
devShells = forAllSystems ({ pkgs }: { devShells = forAllSystems ({ pkgs }:
let
scripts = createServiceScripts pkgs pdsDirectory;
in {
default = pkgs.mkShell { default = pkgs.mkShell {
# The Nix packages provided in the environment # The Nix packages provided in the environment
packages = (with pkgs; [ packages = (with pkgs; [
@ -45,6 +108,14 @@
sqlx-cli sqlx-cli
rustToolchain rustToolchain
]) ++ pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]); ]) ++ pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]);
shellHook = pkgs.lib.optionalString pkgs.stdenv.isLinux ''
# Cleanup
${scripts.stopScript}
# Start the systemd service
${scripts.startScript}
'';
}; };
}); });
}; };