dotfiles

My PC Dotfiles & Website

modules/hosts/wsl/_windows.nix (2983B)


  1## Windows Integration ##
  2_: {
  3  nixos = {pkgs, ...}: {
  4    wsl = {
  5      enable = true;
  6      interop.includePath = false;
  7      docker-desktop.enable = true;
  8      extraBin = map (name: {src = "${pkgs.coreutils}/bin/${name}";}) [
  9        "install"
 10        "mv"
 11        "rm"
 12        "stat"
 13        "test"
 14      ];
 15    };
 16  };
 17
 18  home = {
 19    lib,
 20    pkgs,
 21    osConfig ? null,
 22    ...
 23  }: let
 24    mountRoot =
 25      if osConfig == null
 26      then "/mnt"
 27      else osConfig.wsl.wslConf.automount.root;
 28
 29    system32 = "${mountRoot}/c/Windows/System32";
 30    powershell = "${system32}/WindowsPowerShell/v1.0/powershell.exe";
 31
 32    windowsCommands = pkgs.symlinkJoin {
 33      name = "windows-commands";
 34      paths = lib.mapAttrsToList (name: command:
 35        pkgs.writeShellScriptBin name ''
 36          exec ${lib.escapeShellArg command} "$@"
 37        '') {
 38        "cmd.exe" = "${system32}/cmd.exe";
 39        "explorer.exe" = "${mountRoot}/c/Windows/explorer.exe";
 40        "notepad.exe" = "${system32}/notepad.exe";
 41        "powershell.exe" = powershell;
 42      };
 43    };
 44
 45    copy = pkgs.writeShellScriptBin "win-copy" ''
 46      exec "${powershell}" -NoLogo -NoProfile -NonInteractive -Command \
 47        '[Console]::InputEncoding = [Text.UTF8Encoding]::new(); Set-Clipboard -Value ([Console]::In.ReadToEnd())'
 48    '';
 49
 50    paste = pkgs.writeShellScriptBin "win-paste" ''
 51      exec "${powershell}" -NoLogo -NoProfile -NonInteractive -Command \
 52        '[Console]::OutputEncoding = [Text.UTF8Encoding]::new(); [Console]::Out.Write((Get-Clipboard -Raw))'
 53    '';
 54
 55    clipboard = pkgs.writeShellScriptBin "xclip" ''
 56      for argument in "$@"
 57      do
 58        case "$argument" in
 59          -o|-out) exec ${lib.getExe paste} ;;
 60        esac
 61      done
 62      exec ${lib.getExe copy}
 63    '';
 64
 65    code = pkgs.writeShellScriptBin "code" ''
 66      exec ${lib.getExe pkgs.bash} "${mountRoot}/c/Program Files/Microsoft VS Code/bin/code" "$@"
 67    '';
 68
 69    editor = pkgs.writeShellScriptBin "np" ''
 70      if [ "$#" -eq 0 ]
 71      then
 72        exec "${system32}/notepad.exe"
 73      fi
 74      exec "${system32}/notepad.exe" "$(wslpath -aw "$1")"
 75    '';
 76
 77    open = pkgs.writeShellScriptBin "xdg-open" ''
 78      if [ "$#" -ne 1 ]
 79      then
 80        echo 'Usage: xdg-open FILE_OR_URL' >&2
 81        exit 2
 82      fi
 83      target="$1"
 84      case "$target" in
 85        *://*|mailto:*) ;;
 86        *) target="$(wslpath -aw "$target")" ;;
 87      esac
 88      printf '%s' "$target" | "${powershell}" -NoLogo -NoProfile -NonInteractive -Command \
 89        '[Console]::InputEncoding = [Text.UTF8Encoding]::new(); Start-Process -FilePath ([Console]::In.ReadToEnd())'
 90    '';
 91  in {
 92    home = {
 93      packages = [windowsCommands copy paste clipboard code editor open];
 94      sessionVariables.BROWSER = lib.getExe open;
 95      shellAliases.whome = "cd ${mountRoot}/c/Users";
 96    };
 97
 98    programs = {
 99      git.settings.credential.helper = [];
100      zsh.initContent = ''
101        export GPG_TTY="$(tty)"
102      '';
103    };
104  };
105}