From 6691d3c037a7cfcdf4489c05f045bf2dadd62644 Mon Sep 17 00:00:00 2001 From: Yan Lin Date: Fri, 29 Aug 2025 21:23:13 +0200 Subject: [PATCH] Wrap macos-only config behind platform detection --- modules/lazygit.nix | 6 ++--- modules/nvim.nix | 27 ++++++++++++--------- modules/papis.nix | 41 +++++++++++++++++++++----------- modules/rsync.nix | 10 ++++---- modules/termscp.nix | 4 ++-- modules/zsh.nix | 58 ++++++++++++++++++++++++--------------------- 6 files changed, 85 insertions(+), 61 deletions(-) diff --git a/modules/lazygit.nix b/modules/lazygit.nix index afaaf4f..a129506 100644 --- a/modules/lazygit.nix +++ b/modules/lazygit.nix @@ -1,4 +1,4 @@ -{ config, pkgs, ... }: +{ config, pkgs, lib, ... }: { programs.lazygit = { @@ -334,8 +334,8 @@ # OS settings os = { - open = "open {{filename}}"; - openLink = "open {{link}}"; + open = if pkgs.stdenv.isDarwin then "open {{filename}}" else "xdg-open {{filename}}"; + openLink = if pkgs.stdenv.isDarwin then "open {{link}}" else "xdg-open {{link}}"; }; # Disable startup popup diff --git a/modules/nvim.nix b/modules/nvim.nix index f796b51..e1b964c 100644 --- a/modules/nvim.nix +++ b/modules/nvim.nix @@ -1,4 +1,4 @@ -{ pkgs, ... }: +{ pkgs, lib, ... }: { programs.nixvim = { @@ -265,26 +265,31 @@ } } - -- Unicode-safe file operations for macOS + -- Unicode-safe file operations function open_file_with_system_app() local filepath = vim.fn.expand('%:p') if filepath ~= "" then local escaped_path = vim.fn.shellescape(filepath) - vim.fn.system('open ' .. escaped_path) + ${if pkgs.stdenv.isDarwin then + "vim.fn.system('open ' .. escaped_path)" + else + "vim.fn.system('xdg-open ' .. escaped_path)"} else print("No file to open") end end - function show_file_in_finder() - local filepath = vim.fn.expand('%:p') - if filepath ~= "" then - local escaped_path = vim.fn.shellescape(filepath) - vim.fn.system('open -R ' .. escaped_path) - else - print("No file to show") + ${lib.optionalString pkgs.stdenv.isDarwin '' + function show_file_in_finder() + local filepath = vim.fn.expand('%:p') + if filepath ~= "" then + local escaped_path = vim.fn.shellescape(filepath) + vim.fn.system('open -R ' .. escaped_path) + else + print("No file to show") + end end - end + ''} ''; }; } diff --git a/modules/papis.nix b/modules/papis.nix index 67cd347..779e822 100644 --- a/modules/papis.nix +++ b/modules/papis.nix @@ -1,13 +1,13 @@ -{ pkgs, ... }: +{ pkgs, lib, ... }: { # Papis configuration - home.file."Library/Application Support/papis/config".text = '' + home.file.${if pkgs.stdenv.isDarwin then "Library/Application Support/papis/config" else ".config/papis/config"}.text = '' [settings] default-library = main editor = nvim - opentool = open - file-browser = open + opentool = ${if pkgs.stdenv.isDarwin then "open" else "xdg-open"} + file-browser = ${if pkgs.stdenv.isDarwin then "open" else "xdg-open"} # Document management ref-format = {doc[author]}{doc[year]} @@ -93,16 +93,29 @@ # Shell functions for papis workflow programs.zsh.initContent = '' - # Papis finder function - open document directory in Finder with query support - pafinder() { - local result=$(papis list "$@" | head -1) - if [ -n "$result" ]; then - open -R "$result" - else - echo "No documents found" - return 1 - fi - } + # Papis finder function - open document directory with query support + ${lib.optionalString pkgs.stdenv.isDarwin '' + pafinder() { + local result=$(papis list "$@" | head -1) + if [ -n "$result" ]; then + open -R "$result" + else + echo "No documents found" + return 1 + fi + } + ''} + ${lib.optionalString (!pkgs.stdenv.isDarwin) '' + pafinder() { + local result=$(papis list "$@" | head -1) + if [ -n "$result" ]; then + xdg-open "$(dirname "$result")" + else + echo "No documents found" + return 1 + fi + } + ''} # Papis add file function - add file to existing document with proper parameter handling pafile() { diff --git a/modules/rsync.nix b/modules/rsync.nix index 5f99c12..830f507 100644 --- a/modules/rsync.nix +++ b/modules/rsync.nix @@ -1,4 +1,4 @@ -{ config, pkgs, ... }: +{ config, pkgs, lib, ... }: { # Rsync exclude patterns for common files and directories @@ -40,9 +40,11 @@ --partial --partial-dir=.rsync-partial - # Preserve extended attributes and ACLs (macOS) - --extended-attributes - --acls + ${lib.optionalString pkgs.stdenv.isDarwin '' + # Preserve extended attributes and ACLs (macOS) + --extended-attributes + --acls + ''} # Network optimization --compress diff --git a/modules/termscp.nix b/modules/termscp.nix index 9b36e67..8b2573b 100644 --- a/modules/termscp.nix +++ b/modules/termscp.nix @@ -1,7 +1,7 @@ -{ config, pkgs, ... }: +{ config, pkgs, lib, ... }: { - home.file."Library/Application Support/termscp/config.toml".text = '' + home.file.${if pkgs.stdenv.isDarwin then "Library/Application Support/termscp/config.toml" else ".config/termscp/config.toml"}.text = '' # termscp configuration file # Generated by Nix - see modules/termscp.nix for customization diff --git a/modules/zsh.nix b/modules/zsh.nix index 5f52c26..d785483 100644 --- a/modules/zsh.nix +++ b/modules/zsh.nix @@ -1,4 +1,4 @@ -{ config, pkgs, ... }: +{ config, pkgs, lib, ... }: let projectsConfig = import ../config/projects.nix { homeDirectory = config.home.homeDirectory; }; @@ -21,16 +21,18 @@ in shellAliases = { ll = "ls -alF"; zi = "z -i"; # Interactive selection with fzf - preview = "open -a Preview"; - slide = "open -a SlidePilot"; - pixel = "open -a 'Pixelmator Pro'"; - inkscape = "open -a Inkscape"; # Nix helpers hm = "home-manager"; hms = "home-manager switch --flake ~/.config/nix#yanlin"; hms-offline = "home-manager switch --flake ~/.config/nix#yanlin --option substitute false"; + } // lib.optionalAttrs pkgs.stdenv.isDarwin { + # macOS-specific app aliases + preview = "open -a Preview"; + slide = "open -a SlidePilot"; + pixel = "open -a 'Pixelmator Pro'"; + inkscape = "open -a Inkscape"; }; initContent = '' @@ -115,29 +117,31 @@ in fi } - # Function to search and open all macOS applications - function app() { - local app_path - local file_to_open="$1" - - app_path=$( (find -L /Applications -name "*.app" -maxdepth 2 2>/dev/null; \ - find -L ~/Applications -name "*.app" -maxdepth 3 2>/dev/null; \ - find /System/Applications -name "*.app" -maxdepth 2 2>/dev/null; \ - find /System/Applications/Utilities -name "*.app" -maxdepth 1 2>/dev/null) | - sort | uniq | - fzf --header="Select app to open''${file_to_open:+ file: $file_to_open}" \ - --preview 'basename {} .app' \ - --preview-window=up:1 \ - --height=40%) - - if [[ -n "$app_path" ]]; then - if [[ -n "$file_to_open" ]]; then - open -a "$app_path" "$file_to_open" - else - open "$app_path" + # Function to search and open all macOS applications (macOS only) + ${lib.optionalString pkgs.stdenv.isDarwin '' + function app() { + local app_path + local file_to_open="$1" + + app_path=$( (find -L /Applications -name "*.app" -maxdepth 2 2>/dev/null; \ + find -L ~/Applications -name "*.app" -maxdepth 3 2>/dev/null; \ + find /System/Applications -name "*.app" -maxdepth 2 2>/dev/null; \ + find /System/Applications/Utilities -name "*.app" -maxdepth 1 2>/dev/null) | + sort | uniq | + fzf --header="Select app to open''${file_to_open:+ file: $file_to_open}" \ + --preview 'basename {} .app' \ + --preview-window=up:1 \ + --height=40%) + + if [[ -n "$app_path" ]]; then + if [[ -n "$file_to_open" ]]; then + open -a "$app_path" "$file_to_open" + else + open "$app_path" + fi fi - fi - } + } + ''} # Interactive project launcher with fzf function proj() {