Find out our product. Here!

How I Manage Packages and Home with Nix on MacOS (Darwin)

Nix-darwin, Nix is a powerful package manager for Unix-like operating systems that provides a purely functional and declarative approach to package m
nix-darwin

Nix Home Manager is a component of the Nix package manager ecosystem. Nix is a powerful package manager for Unix-like operating systems that provides a purely functional and declarative approach to package management.

It allows users to define and manage software packages and configurations in a reproducible manner.

Nix Home Manager, specifically, is a tool that extends Nix to manage the configuration of user-specific packages and settings on a per-user basis.

It allows users to define and manage their personal environment, including things like shell configurations, text editor settings, window manager configurations, and other user-specific software packages, all within a user's home directory.

With Nix Home Manager, you can version control your home directory configuration, making it easy to share your settings across different machines and ensuring consistency in your personal environment.

It enables you to keep your user-specific configurations isolated from the system-wide configurations managed by the Nix package manager, thus providing a clean separation between system-level and user-level configurations.

By using Nix Home Manager in combination with the Nix package manager, you can create reproducible and portable development environments across different machines, making it a popular choice among developers who value the benefits of functional programming and declarative package management.

Why Consider Nix-Darwin for macOS?

  1. Reproducibility:

    • Your entire system configuration is stored in a Git repository.
    • Easily replicate your preferred setup on a new computer by running a simple darwin-rebuild command.
  2. Reusability:

    • Effortlessly import your configurations (shell, editor, tmux) to maintain a consistent experience across different machines.
    • Seamlessly apply configurations to both your laptop and servers for a familiar and efficient workflow.
  3. Streamlined Package Management:

    • Gain a clear understanding of your installed packages and avoid cluttering storage with forgotten or unnecessary packages.

My Setup

nix-darwinSimple Nix Darwin Configurations

My configured Nix-Darwin setup is relatively simple yet more than sufficient to meet my needs. The configuration encompasses various aspects of MacOS system and services, such as the dock, Safari, Finder, window manager, and more.

Additionally, it extends to home directory configurations, including shell preferences, Git settings, Starship customization, development environments, and other essential components.

With Nix-Darwin, managing and maintaining these preferences across different devices is effortless, ensuring a consistent and efficient user experience.

User Custom Preferences

System Level Preference

At the system level, Nix-Darwin allows me to tailor settings and configurations for MacOS features like the dock, Safari, and Finder.

For instance, I can customize the appearance and behavior of the dock to suit my workflow, optimize Safari settings for improved browsing, and fine-tune Finder's functionalities based on my preferences. These configurations collectively create a personalized system environment.

Yabai Window Manager
image_title

In certain usage scenarios, having a window manager equipped with powerful window tiling features significantly enhances my workflow. One such instance is when I'm working on MacOS, where I make use of the Yabai window manager.

Yabai allows me to efficiently organize and arrange my application windows, maximizing my productivity by ensuring a clutter-free and organized workspace. Its tiling capabilities, alongside other features, make it a valuable tool in my toolkit

In conjunction with Yabai, I utilize skhd to define and manage custom keyboard shortcuts tailored to my preferences and specific needs. These shortcuts serve as quick actions that further streamline my interactions with the window manager and applications.

By combining Yabai's window management prowess with skhd's customized shortcuts, I've crafted an environment that empowers me to navigate and control my MacOS system with precision and speed, ultimately optimizing my overall efficiency and productivity.

{
  description = "Kemalmao minimal flake for Darwin";
  inputs = {
    # Where we get most of our software. Giant mono repo with recipes
    # called derivations that say how to build software.
    nixpkgs = {
      url = "github:nixos/nixpkgs/nixpkgs-unstable"; # nixos-22.11
    };

    # Manages configs links things into your home directory
    home-manager = {
      url = "github:nix-community/home-manager/master";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    # Controls system level software and settings including fonts
    darwin = {
      url = "github:lnl7/nix-darwin";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    # Tricked out nvim
    # pwnvim.url = "github:zmre/pwnvim";
  };


  outputs = inputs@{ nixpkgs, home-manager, darwin, ... }: {
    darwinConfigurations.kemalmao = darwin.lib.darwinSystem {
      system = "x86_64-darwin";
      pkgs = import nixpkgs { 
        system = "x86_64-darwin"; 
        config = { 
          allowUnfree = true;
          # allowUnsupportedSystem = true;
          # allowBroken = true;
          };
        };
       
      modules = [
        ./modules/darwin
        
        home-manager.darwinModules.home-manager
        {
          home-manager = {
            useGlobalPkgs = true;
            useUserPackages = true;
            # extraSpecialArgs = { inherit pwnvim; };
            users.kemalmao.imports = [ ./modules/home-manager ];
          };
        }
      ];
    };
  };
}
{ pkgs, ... }: {
  # here go the darwin preferences and config items
  programs.fish.enable = true;
  programs.zsh.enable = true;
  environment = {
    # shells = with pkgs; [ bash zsh fish ];
    # loginShell = pkgs.fish;
    systemPackages = [ pkgs.coreutils ];
    systemPath = [ "/opt/homebrew/bin" ];
    pathsToLink = [ "/Applications" ];
  };

  nix.extraOptions = ''
    experimental-features = nix-command flakes
  '';

  system.activationScripts.postUserActivation.text = ''
    # Following line should allow us to avoid a logout/login cycle
    /System/Library/PrivateFrameworks/SystemAdministration.framework/Resources/activateSettings -u
  '';

  imports = [ 
    # Window Manager
    # ./services/wm/kwm.nix 
    ./services/wm/yabai.nix
    ./services/wm/skhd.nix
    # ./services/wm/spacebar.nix

    # Homebrew
    ./services/brew.nix

    # Custom Preferences
    ./services/custom_preferences.nix

    # Main Darwin
    ./services/main_darwin.nix

    # database sql
    # ./services/postgresql.nix
    ];

  # backwards compat; don't change
  system.stateVersion = 4;

}
{ pkgs, ... }: {
  #YABAI Window Manager
  services.yabai = {
    enable = true;
    config = {
      focus_follows_mouse = "autoraise";
      # mouse_follows_focus = "off";
      window_placement = "second_child";
      window_opacity = "off";
      window_opacity_duration = "0.0";
      window_border = "on";
      window_border_placement = "inset";
      window_border_width = 2;
      window_border_radius = 9;
      active_window_border_topmost = "off";
      window_topmost = "on";
      window_shadow = "float";
      active_window_border_color = "0xff5c7e81";
      normal_window_border_color = "0xff505050";
      insert_window_border_color = "0xffd75f5f";
      active_window_opacity = "1.0";
      normal_window_opacity = "1.0";
      split_ratio = "0.50";
      auto_balance = "on";
      mouse_modifier = "fn";
      mouse_action1 = "move";
      mouse_action2 = "resize";
      layout = "bsp";
      top_padding = 10;
      bottom_padding = 10;
      left_padding = 10;
      right_padding = 10;
      window_gap = 10;
    };
    extraConfig = ''
      yabai -m rule --add app='System Preferences' manage=off
    '';
    # enableScriptingAddition = true;
  };
}

    

Home Level Preference

On the home level, Nix-Darwin enables me to define specific configurations within my home directory. This includes setting up my preferred shell and its configurations, managing Git settings for version control, customizing the Starship prompt to enhance the command-line experience, and configuring development environments tailored to my projects.

These configurations, rooted in my home directory, play a pivotal role in optimizing my day-to-day activities and ensuring a seamless development process.

image_title

If youre interested to use or see all my configuration, you can visit me on github

Discover more insightful tips and tricks by visiting my YouTube channel!

Read Also :
Holla, we share any interesting view for perspective and education sharing❤️

Post a Comment

© elgharuty. All rights reserved. Developed by Jago Desain