Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@

# macOS
.DS_Store/

# Nix
result
27 changes: 27 additions & 0 deletions flake.lock

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

131 changes: 131 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
description = "Glide is a tiling window manager for macOS.";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};

outputs = { self, nixpkgs }:
let
lib = nixpkgs.lib;
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
packagerMeta = cargoToml.package.metadata.packager;
productName = packagerMeta.productName;
bundleName = "${productName}.app";
bundleIdentifier = packagerMeta.identifier;
version = cargoToml.package.version;
supportedSystems = [
"aarch64-darwin"
"x86_64-darwin"
];
forAllSystems = f:
lib.genAttrs supportedSystems (
system: f (import nixpkgs { inherit system; })
);
Comment on lines +21 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using import nixpkgs { inherit system; } inside lib.genAttrs can be inefficient as it re-evaluates the nixpkgs import for each system. A more idiomatic and efficient approach in flakes is to use nixpkgs.legacyPackages.${system}.

      forAllSystems = f:
        lib.genAttrs supportedSystems (
          system: f nixpkgs.legacyPackages.${system}
        );

mkGlide = pkgs:
pkgs.rustPlatform.buildRustPackage {
pname = cargoToml.package.name;
inherit version;

src = pkgs.lib.cleanSource ./.;

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

doCheck = false;

meta = with pkgs.lib; {
description = cargoToml.package.description;
homepage = cargoToml.package.homepage;
license = [
licenses.asl20
licenses.mit
];
mainProgram = "glide";
platforms = platforms.darwin;
};
};
mkGlideBundle = pkgs: unbundled:
pkgs.runCommand "${cargoToml.package.name}-app-${version}" {
meta = with pkgs.lib; {
description = "${cargoToml.package.description} (${bundleName})";
homepage = cargoToml.package.homepage;
license = [
licenses.asl20
licenses.mit
];
mainProgram = "glide";
platforms = platforms.darwin;
};
} ''
bundle="$out/Applications/${bundleName}"

mkdir -p "$bundle/Contents/MacOS" "$out/bin"

install -m755 ${unbundled}/bin/glide "$bundle/Contents/MacOS/glide"
install -m755 ${unbundled}/bin/glide_server "$bundle/Contents/MacOS/glide_server"

printf '%s\n' \
'<?xml version="1.0" encoding="UTF-8"?>' \
'<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' \
'<plist version="1.0">' \
' <dict>' \
' <key>CFBundleDevelopmentRegion</key>' \
' <string>en</string>' \
' <key>CFBundleExecutable</key>' \
' <string>glide_server</string>' \
' <key>CFBundleIdentifier</key>' \
' <string>${bundleIdentifier}</string>' \
' <key>CFBundleInfoDictionaryVersion</key>' \
' <string>6.0</string>' \
' <key>CFBundleName</key>' \
' <string>${productName}</string>' \
' <key>CFBundlePackageType</key>' \
' <string>APPL</string>' \
' <key>CFBundleShortVersionString</key>' \
' <string>${version}</string>' \
' <key>CFBundleVersion</key>' \
' <string>${version}</string>' \
' <key>NSPrincipalClass</key>' \
' <string>NSApplication</string>' \
' </dict>' \
'</plist>' \
> "$bundle/Contents/Info.plist"

printf 'APPL????' > "$bundle/Contents/PkgInfo"

ln -s "../Applications/${bundleName}/Contents/MacOS/glide" "$out/bin/glide"
ln -s "../Applications/${bundleName}/Contents/MacOS/glide_server" "$out/bin/glide_server"

printf '%s\n' \
'#!/bin/sh' \
"exec \"$out/bin/glide\" launch \"\$@\"" \
> "$out/bin/glide-launch"
chmod +x "$out/bin/glide-launch"
'';
Comment on lines +50 to +107

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The mkGlideBundle function can be made more robust and maintainable by using some standard Nix helper functions.

  • Explicitly declare nativeBuildInputs like makeWrapper and coreutils for tools used in the script.
  • Generate the Info.plist file using pkgs.writeText outside the script to separate data from logic.
  • Use pkgs.makeWrapper to create the glide-launch script, which is the standard way to create wrapper scripts in Nix.

Here's a suggested refactoring that applies these improvements:

      mkGlideBundle = pkgs: unbundled:
        let
          infoPlist = pkgs.writeText "Info.plist" ''
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
            <plist version="1.0">
              <dict>
                <key>CFBundleDevelopmentRegion</key>
                <string>en</string>
                <key>CFBundleExecutable</key>
                <string>glide_server</string>
                <key>CFBundleIdentifier</key>
                <string>${bundleIdentifier}</string>
                <key>CFBundleInfoDictionaryVersion</key>
                <string>6.0</string>
                <key>CFBundleName</key>
                <string>${productName}</string>
                <key>CFBundlePackageType</key>
                <string>APPL</string>
                <key>CFBundleShortVersionString</key>
                <string>${version}</string>
                <key>CFBundleVersion</key>
                <string>${version}</string>
                <key>NSPrincipalClass</key>
                <string>NSApplication</string>
              </dict>
            </plist>
          '';
        in
        pkgs.runCommand "${cargoToml.package.name}-app-${version}" {
          nativeBuildInputs = [ pkgs.makeWrapper pkgs.coreutils ];
          meta = with pkgs.lib; {
            description = "${cargoToml.package.description} (${bundleName})";
            homepage = cargoToml.package.homepage;
            license = [
              licenses.asl20
              licenses.mit
            ];
            mainProgram = "glide";
            platforms = platforms.darwin;
          };
        } ''
          bundle="$out/Applications/${bundleName}"

          mkdir -p "$bundle/Contents/MacOS" "$out/bin"

          install -m755 ${unbundled}/bin/glide "$bundle/Contents/MacOS/glide"
          install -m755 ${unbundled}/bin/glide_server "$bundle/Contents/MacOS/glide_server"

          cp ${infoPlist} "$bundle/Contents/Info.plist"

          printf 'APPL????' > "$bundle/Contents/PkgInfo"

          ln -s "../Applications/${bundleName}/Contents/MacOS/glide" "$out/bin/glide"
          ln -s "../Applications/${bundleName}/Contents/MacOS/glide_server" "$out/bin/glide_server"

          makeWrapper "$out/bin/glide" "$out/bin/glide-launch" --add-flags "launch"
        '';

in
{
packages = forAllSystems (
pkgs:
let
unbundled = mkGlide pkgs;
glide = mkGlideBundle pkgs unbundled;
in
{
default = glide;
"glide-wm" = glide;
bundle = glide;
inherit unbundled;
}
);

apps = forAllSystems (pkgs: {
default = {
type = "app";
program = "${self.packages.${pkgs.stdenv.hostPlatform.system}.default}/bin/glide-launch";
};
});
};
}