flake.nix 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. {
  2. description = "Firefox";
  3. inputs = {
  4. flake-compat = {
  5. url = "github:edolstra/flake-compat";
  6. flake = false;
  7. };
  8. home-manager = {
  9. url = "github:nix-community/home-manager";
  10. inputs.nixpkgs.follows = "nixpkgs";
  11. };
  12. };
  13. outputs = { self, nixpkgs, home-manager, ... }:
  14. let
  15. system = "x86_64-linux";
  16. pkgs = import nixpkgs {
  17. inherit system;
  18. };
  19. in {
  20. devShell.x86_64-linux = with pkgs; mkShell {
  21. buildInputs = [ nixFlakes ];
  22. };
  23. packages.${system} = with pkgs; rec {
  24. default = firefox-wrapper;
  25. firefox-wrapper =
  26. callPackage ({ stdenv, firefox, nix }:
  27. writeScriptBin "firefox" ''
  28. #!${runtimeShell} -e
  29. set -x
  30. PATH="${stunnel}/bin:${nix}/bin:${gnused}/bin:${git}/bin:${utillinux}/bin:${ncurses}/bin:${coreutils}/bin:$PATH"
  31. export PATH
  32. temp_directory="$(mktemp --directory /tmp/firefox.XXXXXXXXXX)"
  33. mkdir -p "$temp_directory/src"
  34. cp --recursive ${./.} "$temp_directory/src/flake"
  35. trap 'chmod -Rf +w "$temp_directory"; rm -rf "$temp_directory"' EXIT
  36. export temp_directory
  37. (
  38. cd "$temp_directory/src/flake" || exit 1
  39. chmod 0644 flake.nix
  40. chmod 755 .
  41. sed --in-place "s|nonexistentHome|$HOME|;s|nonexistentUser|''${USER:-root}|" flake.nix
  42. sed --in-place "s|@javaws@|${adoptopenjdk-icedtea-web}|" mimeTypes.rdf
  43. git init
  44. git add -A
  45. git config user.email "you@example.com"
  46. git config user.name "Your Name"
  47. git commit -m 'Initial commit.'
  48. nix run .#home-manager -- build --flake .#firefox
  49. )
  50. profile="$(readlink -f $temp_directory/src/flake/result)"
  51. export profile
  52. run_firefox()
  53. {
  54. USER=''${USER:-root} $profile/activate
  55. FONTCONFIG_FILE=${fontconfig.out}/etc/fonts/fonts.conf
  56. export FONTCONFIG_FILE
  57. mkdir -p $HOME/.config
  58. echo 'application/x-java-jnlp-file=javaws.desktop;' > $HOME/.config/mimeapps.list
  59. ${firefox}/bin/firefox --new-instance --profile "$HOME/.mozilla/firefox/tmp" "$@"
  60. }
  61. export -f run_firefox
  62. run_firefox_without_home()
  63. {
  64. # Make directories empty to preserve user's files
  65. mount --types tmpfs none "$HOME"
  66. mount --types tmpfs none "/nix/var/nix/profiles/per-user/''${USER:-root}"
  67. run_firefox "$@"
  68. }
  69. export -f run_firefox_without_home
  70. if [ -f /.dockerenv ]
  71. then
  72. exec -a "$0" sh -c "set -e; run_firefox $@"
  73. else
  74. exec -a "$0" unshare --mount --map-root-user --fork sh -c "set -e; run_firefox_without_home $@"
  75. fi
  76. '') { firefox = firefox; nix = pkgs.nixFlakes; };
  77. };
  78. homeConfigurations = {
  79. firefox = home-manager.lib.homeManagerConfiguration {
  80. inherit pkgs system;
  81. homeDirectory = "nonexistentHome";
  82. username = "nonexistentUser";
  83. configuration.imports = [ ./home.nix ];
  84. };
  85. };
  86. apps.${system} = {
  87. inherit (home-manager.apps.${system}) home-manager;
  88. firefox = {
  89. type = "app";
  90. program = "${self.packages.${system}.firefox-wrapper}/bin/firefox";
  91. };
  92. };
  93. defaultApp.${system} = self.apps.${system}.firefox;
  94. };
  95. }