Skip to content

Commit

Permalink
refactor: improve nix infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
ratsclub authored and kmaasrud committed Aug 8, 2024
1 parent e963629 commit f61a370
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 209 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/nix.yml → .github/workflows/nix-build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Nix
name: Nix build

on:
push:
Expand All @@ -12,13 +12,11 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Install Nix
uses: cachix/install-nix-action@v15
with:
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
uses: DeterminateSystems/nix-installer-action@main
- name: Install Nix magic cache
uses: DeterminateSystems/magic-nix-cache-action@main
- name: Build
run: nix build
23 changes: 23 additions & 0 deletions .github/workflows/nix-flake-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Update flake.lock
on:
workflow_dispatch: # allows manual triggering
schedule:
- cron: '0 0 * * 1' # runs weekly on Monday at 00:00

jobs:
lockfile:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@main
- name: Install Nix magic cache
uses: DeterminateSystems/magic-nix-cache-action@main
- name: Update flake.lock
uses: DeterminateSystems/update-flake-lock@main
with:
pr-title: "chore: update flake.lock" # Title of PR to be created
pr-labels: | # Labels to be set on the PR
dependencies
automated
9 changes: 4 additions & 5 deletions .github/workflows/website-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2.3.1
uses: actions/checkout@v4

- name: Install Nix
uses: cachix/install-nix-action@v15
with:
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
uses: DeterminateSystems/nix-installer-action@main
- name: Install Nix magic cache
uses: DeterminateSystems/magic-nix-cache-action@main

- name: Build
run: nix build .#website
Expand Down
115 changes: 7 additions & 108 deletions flake.lock

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

119 changes: 68 additions & 51 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,82 @@
description = "Plain text Zettelkasten based on mdBook";

inputs = {
utils.url = "github:numtide/flake-utils";
rust.url = "github:oxalica/rust-overlay";
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
};

outputs = {
self,
nixpkgs,
utils,
rust,
}: let
pname = "mdzk";
version =
(builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version;
in
{
overlays.default = nixpkgs.lib.composeManyExtensions [
rust.overlays.default
(final: _: {
customRustToolchain =
final.rust-bin.selectLatestNightlyWith
(toolchain:
toolchain.default.override {
extensions = ["rust-std" "rust-src"];
});
outputs =
{ self
, nixpkgs
}:
let
forAllSystems = nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed;

mdzk = import ./nix/package.nix {
inherit pname version;
pkgs = final;
};
})
];
}
// utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs {
nixpkgsFor = forAllSystems (system: import nixpkgs {
inherit system;
overlays = [self.overlays.default];
overlays = [ self.overlays.default ];
});

pname = "mdzk";
version =
(builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version;
in
{
overlays.default = final: prev: {
mdzk = prev.callPackage ./nix/package.nix { inherit prev pname version; };
};

inherit (pkgs) mdzk;
in rec {
# `nix build .#mdzk`
packages.${pname} = mdzk;
apps = forAllSystems (system:
let
pkgs = nixpkgsFor."${system}";
app = {
type = "app";
program = "${pkgs.mdzk}/bin/mdzk";
};
in
{
# `nix run`
default = app;

# `nix run .#mdzk`
mdzk = app;
});

# `nix build .#website`
packages.website = pkgs.callPackage ./nix/website.nix {inherit pkgs;};
packages = forAllSystems (system:
let
pkgs = nixpkgsFor."${system}";
in
{
# `nix build`
default = pkgs.mdzk;

# `nix build`
packages.default = packages.${pname};
# `nix build .#mdzk`
"${pname}" = pkgs.mdzk;

# `nix run`
apps.${pname} = utils.lib.mkApp {drv = packages.${pname};};
apps.default = apps.${pname};
# `nix build .#website`
website = pkgs.callPackage ./nix/website.nix { };
});

# `nix develop`
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
# rust
customRustToolchain
];
};
});
formatter = forAllSystems (system:
let
pkgs = nixpkgsFor."${system}";
in
# `nix fmt`
pkgs.nixpkgs-fmt);

devShells = forAllSystems (system:
let
pkgs = nixpkgsFor."${system}";

inherit (pkgs)
mkShell
cargo
rustc
rust-analyzer;
in
{
default = mkShell {
buildInputs = [ cargo rust-analyzer rustc ];
};
});
};
}
52 changes: 25 additions & 27 deletions nix/package.nix
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
{
pkgs,
pname,
version,
...
}: let
rustPlatform = pkgs.makeRustPlatform {
rustc = pkgs.customRustToolchain;
cargo = pkgs.customRustToolchain;
};
{ pkgs
, pname
, version
, ...
}:
let
inherit (pkgs)
rustPlatform;
in
rustPlatform.buildRustPackage {
inherit pname version;
rustPlatform.buildRustPackage {
inherit pname version;

src = pkgs.lib.cleanSource ../.;
src = pkgs.lib.cleanSource ../.;

buildInputs =
pkgs.lib.optionals pkgs.stdenv.isDarwin
[pkgs.darwin.apple_sdk.frameworks.CoreServices];
buildInputs =
pkgs.lib.optionals pkgs.stdenv.isDarwin
[ pkgs.darwin.apple_sdk.frameworks.CoreServices ];

makeFlags = ["PREFIX=$(out)"];
makeFlags = [ "PREFIX=$(out)" ];

postBuild = ''
pandoc --standalone --to man public/man.md -o public/mdzk.1
'';
postBuild = ''
pandoc --standalone --to man public/man.md -o public/mdzk.1
'';

preInstall = ''
install -d $out/share/man/man1/
install -pm 0644 public/mdzk.1 $out/share/man/man1/
'';
preInstall = ''
install -d $out/share/man/man1/
install -pm 0644 public/mdzk.1 $out/share/man/man1/
'';

nativeBuildInputs = with pkgs; [pandoc];
nativeBuildInputs = with pkgs; [ pandoc ];

cargoSha256 = "sha256-2exNoGAj2ndVRu90tCv+CEn3nPSBtcjHzJcPkZKBndA=";
}
cargoHash = "sha256-2exNoGAj2ndVRu90tCv+CEn3nPSBtcjHzJcPkZKBndA=";
}
Loading

0 comments on commit f61a370

Please sign in to comment.