Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nix flake #561

Closed
wants to merge 2 commits into from
Closed
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
43 changes: 43 additions & 0 deletions .github/workflows/nix-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Nix

on:
push:
branches: [master]
pull_request:

jobs:
Check:
# multi platform checks aren't really needed here;
# this is mainly to debug evaluation errors
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v9

- name: Setup Nix Cache
uses: DeterminateSystems/magic-nix-cache-action@v2

- name: Run Check
run: nix flake check -L --accept-flake-config --show-trace

Build:
strategy:
matrix:
os: [macos-latest, ubuntu-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v9

- name: Setup Nix Cache
uses: DeterminateSystems/magic-nix-cache-action@v2

- name: Run Build
run: nix build -L --accept-flake-config --fallback
29 changes: 29 additions & 0 deletions .github/workflows/update-flake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Update flake.lock

on:
schedule:
# run every saturday
- cron: "0 0 * * 6"
workflow_dispatch:

jobs:
update:
runs-on: ubuntu-latest

permissions:
contents: write
pull-requests: write

steps:
- uses: actions/checkout@v4

- name: Install Nix
uses: nixbuild/nix-quick-install-action@v26

- name: Update lockfile & make PR
uses: DeterminateSystems/update-flake-lock@v20
id: update
with:
commit-msg: "flake: update inputs"
pr-title: "flake: update inputs"
token: ${{ github.token }}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ fabric.properties

theseus.iml

# nix build results
result*
repl-result-out*

# nix dev shell
.direnv
26 changes: 26 additions & 0 deletions flake.lock

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

101 changes: 101 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
description = "Modrinth's game launcher";

inputs.nixpkgs.url = "nixpkgs/nixpkgs-unstable";

outputs = {
nixpkgs,
self,
...
}: let
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];

forSystem = system: fn:
fn (import nixpkgs {
inherit system;
config.allowUnfree = true;
});

forAllSystems = fn: nixpkgs.lib.genAttrs systems (system: forSystem system fn);
in {
checks = forAllSystems ({
pkgs,
lib,
...
}: {
rustfmt =
pkgs.runCommand "check-rustfmt" {
nativeBuildInputs = with pkgs; [cargo rustfmt];
} ''
cd ${./.}
cargo fmt -- --check
touch $out
'';

alejandra = pkgs.runCommand "check-alejandra" {} ''
${lib.getExe pkgs.alejandra} --check ${./.}
touch $out
'';
});

devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
packages = with pkgs; [
# base toolchain
cargo
rustc

# toolchain utils
clippy
rustfmt
rust-analyzer

# nix utils
self.formatter.${pkgs.system}
nil
];

RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
};
});

formatter = forAllSystems (pkgs: pkgs.alejandra);

overlays.default = final: prev: {
modrinth-app-unwrapped = prev.callPackage ./nix {
version = builtins.substring 0 7 self.rev or "dirty";

inherit
(final.darwin.apple_sdk.frameworks)
AppKit
CoreServices
Security
WebKit
;

inherit (final.nodePackages) pnpm;
};

modrinth-app = prev.callPackage ./nix/wrapper.nix {
inherit (final) modrinth-app-unwrapped;
};
};

packages = forAllSystems (pkgs: let
pkgs' = self.overlays.default (pkgs // pkgs') pkgs;
in {
inherit
(pkgs')
modrinth-app-unwrapped
modrinth-app
;

default = pkgs'.modrinth-app;
});
};
}
163 changes: 163 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
lib,
version,
stdenv,
stdenvNoCC,
fetchFromGitHub,
rustPlatform,
buildGoModule,
makeDesktopItem,
copyDesktopItems,
AppKit,
CoreServices,
Security,
WebKit,
cacert,
pnpm,
esbuild,
dbus,
freetype,
gtk3,
jq,
libappindicator-gtk3,
librsvg,
libsoup,
moreutils,
openssl,
pkg-config,
webkitgtk,
}:
rustPlatform.buildRustPackage rec {
pname = "modrinth-app-unwrapped";
inherit version;

src = lib.cleanSource ../.;

cargoLock = {
lockFile = ../Cargo.lock;
allowBuiltinFetchGit = true;
};

pnpm-deps = stdenvNoCC.mkDerivation {
pname = "${pname}-pnpm-deps";
inherit src version;

nativeBuildInputs = [
cacert
jq
moreutils
pnpm
];

# https://github.com/NixOS/nixpkgs/blob/763e59ffedb5c25774387bf99bc725df5df82d10/pkgs/applications/misc/pot/default.nix#L56
installPhase = ''
export HOME=$(mktemp -d)

cd theseus_gui
pnpm config set store-dir $out
pnpm install --frozen-lockfile --no-optional --ignore-script

rm -rf $out/v3/tmp
for f in $(find $out -name "*.json"); do
sed -i -E -e 's/"checkedAt":[0-9]+,//g' $f
jq --sort-keys . $f | sponge $f
done
'';

dontFixup = true;
outputHashMode = "recursive";
outputHash = "sha256-gRQfWrAY/2XxiVSHtQd4YKruJWjkpAB5OsXZMmV0iDs=";
};

buildInputs =
[openssl]
++ lib.optionals stdenv.isLinux [
dbus
freetype
gtk3
libappindicator-gtk3
librsvg
libsoup
webkitgtk
]
++ lib.optionals stdenv.isDarwin [
AppKit
CoreServices
Security
WebKit
];

nativeBuildInputs = [
pkg-config
pnpm
copyDesktopItems
];

ESBUILD_BINARY_PATH = lib.getExe (
esbuild.override {
buildGoModule = args:
buildGoModule (args
// rec {
version = "0.17.19";
src = fetchFromGitHub {
owner = "evanw";
repo = "esbuild";
rev = "v${version}";
hash = "sha256-PLC7OJLSOiDq4OjvrdfCawZPfbfuZix4Waopzrj8qsU=";
};
vendorHash = "sha256-+BfxCyg0KkDQpHt/wycy/8CTG6YBA/VJvJFhhzUnSiQ=";
});
}
);

preBuild = ''
export HOME=$(mktemp -d)
export STORE_PATH=$(mktemp -d)
pushd theseus_gui

cp -r ${pnpm-deps}/* "$STORE_PATH"
chmod -R +w "$STORE_PATH"

pnpm config set store-dir "$STORE_PATH"
pnpm install --offline --frozen-lockfile --no-optional --ignore-script
pnpm build

popd
'';

desktopItems = [
(makeDesktopItem rec {
name = "com.modrinth.ModrinthApp";
exec = "theseus_gui";
icon = "com.modrinth.ModrinthApp";
desktopName = "Modrinth App";
genericName = desktopName;
comment = meta.description;
terminal = false;
startupNotify = true;
startupWMClass = "ModrinthApp";
categories = ["Game" "ActionGame" "AdventureGame" "Simulation"];
keywords = ["game" "minecraft" "mc"];
})
];

postInstall = lib.optionalString stdenv.isLinux ''
mkdir -p $out/share/{applications,icons/hicolor/256x256/apps}
copyDesktopItems
cp theseus_gui/src-tauri/icons/Square284x284Logo.png $out/share/icons/hicolor/256x256/apps/com.modrinth.ModrinthApp.png
'';

meta = with lib; {
mainProgram = "theseus_gui";
description = "Modrinth's game launcher";
longDescription = ''
Modrinth's game launcher which can be used as a CLI, GUI, and a library for creating and playing Modrinth projects.
'';
homepage = "https://modrinth.com";
license = with licenses; [gpl3Plus unfreeRedistributable];
maintainers = with maintainers; [getchoo];
platforms = with platforms; linux ++ darwin;
# this builds on aarch64, but the launcher itself does not support it yet
broken = stdenv.isAarch64;
};
}
Loading