How to import into main home.nix?

How can I import and built the script in ./miscellaneous/scripts.nix along with the main home.nix?

  imports = [
    ./miscellaneous/scripts.nix
  ];

The above import does not work.

error: The option `environment' does not exist. Definition values:
       - In `/nix/store/np3cpm4pz2d4h15gjnhjd9jaf0466d3c-source/miscellaneous/scripts.nix':
           {
             systemPackages = [
               <derivation my-host-info>
           ...

I read that the environment does not exist. But, how do I export the script to the main home.nix?

Given, home.nix has the following content.

{ config, pkgs, ... }:
let
  isLinux = pkgs.stdenv.hostPlatform.isLinux;
  isDarwin = pkgs.stdenv.hostPlatform.isDarwin;

  isAarch64 = pkgs.stdenv.hostPlatform.isAarch64;
  isx86_64 = pkgs.stdenv.hostPlatform.isx86_64;
  is64Bit = pkgs.stdenv.hostPlatform.is64Bit;

  unsupported = builtins.abort "Unsupported platform";
in
{
  imports = [
    
  ];

  home.username =
    if isLinux then
      "CHANGEME"
    else if isDarwin && isAarch64 then
      "CHANGEME"
    else if isDarwin && isx86_64 then
      "CHANGEME"
    else
      unsupported;
  home.homeDirectory =
    if isLinux then
      "/home/CHANGEME"
    else if isDarwin && isAarch64 then
      "/Users/CHANGEME"
    else if isDarwin && isx86_64 then
      "/Users/CHANGEME"
    else
      unsupported;

  home.stateVersion = "24.05";

  nixpkgs.config.allowUnfree = true;

  home.packages =
    with pkgs;
    (
      [
        atuin
        
        (nerdfonts.override {
          fonts = [
            "DejaVuSansMono"
          ];
        })

        (writeShellScriptBin "my-hello" ''
          echo $(date --universal --iso-8601=seconds)
          echo '----------------'
          echo "Hello, ${config.home.username}!"
          echo '----------------'
        '')
      ]
      ++ lib.optionals isLinux [
        util-linux
      ]
      ++ lib.optionals isDarwin [
        pinentry_mac
      ]
      ++ lib.optionals (isDarwin && stdenv.isx86_64) [
        # macOS packages on Intel processors
        hexdump
      ]
      ++ lib.optionals (stdenv.isDarwin && stdenv.isAarch64) [
        # macOS packages on Apple Silicon processors
      ]
    );

  home.file = {
    
    ".gradle/gradle.properties".text = ''
      org.gradle.console=verbose
      org.gradle.daemon.idletimeout=3600000
    '';

  };
  
  home.sessionVariables = {
    EDITOR = "nvim";
  };

  programs.home-manager.enable = true;
}

And, ./miscellaneous/scripts.nix has the following content.

{
  pkgs,
  lib,
  config,
  ...
}:
let
  my-host-info = pkgs.writeScriptBin "my-host-info" ''
    #!${pkgs.stdenv.shell}

    LOCAL_IP=$(ip -o addr show | grep 'inet ' | grep -v 127.0.0.1 | awk '{print $4}' | cut -d'/' -f 1)
    PUBLIC_IP=$(curl -s ifconfig.me)
    CPU=$(sudo lshw -short | grep -i processor | sed 's/\s\s*/ /g' | cut -d' ' -f3-)
    VIDEO=$(sudo lspci | grep -i --color 'vga\|3d\|2d' | cut -d' ' -f2-)

    echo -e "local: $LOCAL_IP, public: $PUBLIC_IP\n"
    echo -e "Processor: $CPU"
    echo -e "Video: $VIDEO\n"

    echo -e "\n"
    lsblk -f

    echo -e "\n"
    lsmod | rg kvm
  '';
in
{
  environment.systemPackages = [
    my-host-info
  ];
}

environment.systemPackages is a NixOS option, so it can’t be used in Home Manager configurations, you may want to use home.packages instead.

1 Like

Thanks, @FedericoSchonborn! :bowing_man: