NixOS config

Zsh, 4

.

# Edit this configuration file to define what should be installed on your system.  Help is available in the configuration.nix(5) man page and in the NixOS manual (accessible by running ‘nixos-help’).

{ config, pkgs, ... }:

{
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];

  # Bootloader.
  boot.loader.grub.enable = true;
  boot.loader.grub.device = "/dev/sda";
  boot.loader.grub.useOSProber = true;

  networking.hostName = "hi"; # Define your hostname.
  # networking.wireless.enable = true;  # Enables wireless support via wpa_supplicant.
  networking.wireless.enable = false;
  hardware.bluetooth.enable = false;

  # Configure network proxy if necessary
  # networking.proxy.default = "http://user:password@proxy:port/";
  # networking.proxy.noProxy = "127.0.0.1,localhost,internal.domain";

  # Enable networking
  networking.networkmanager.enable = true;

  # Set your time zone.
  time.timeZone = "Australia/Perth";

  # Select internationalisation properties.
  i18n.defaultLocale = "en_US.UTF-8";

  i18n.extraLocaleSettings = {
    LC_ADDRESS = "en_GB.UTF-8";
    LC_IDENTIFICATION = "en_GB.UTF-8";
    LC_MEASUREMENT = "en_GB.UTF-8";
    LC_MONETARY = "en_GB.UTF-8";
    LC_NAME = "en_GB.UTF-8";
    LC_NUMERIC = "en_GB.UTF-8";
    LC_PAPER = "en_GB.UTF-8";
    LC_TELEPHONE = "en_GB.UTF-8";
    LC_TIME = "en_GB.UTF-8";
  };

  # Enable the X11 windowing system.
  services.xserver.enable = true;

  # Enable the KDE Plasma Desktop Environment.
  services.xserver.displayManager.sddm.enable = true;
  services.xserver.desktopManager.plasma5.enable = true;

  # Configure keymap in X11
  services.xserver = {
    layout = "us";
    xkbVariant = "";
  };

  # Enable CUPS to print documents.
  services.printing.enable = true;

  # Enable sound with pipewire.
  sound.enable = true;
  hardware.pulseaudio.enable = false;
  security.rtkit.enable = true;
  services.pipewire = {
    enable = true;
    alsa.enable = true;
    alsa.support32Bit = true;
    pulse.enable = true;
    # If you want to use JACK applications, uncomment this
    #jack.enable = true;

    # use the example session manager (no others are packaged yet so this is enabled by default, no need to redefine it in your config for now)
    #media-session.enable = true;
  };

  # Enable touchpad support (enabled default in most desktopManager).
  # services.xserver.libinput.enable = true;

  # Define a user account. Don't forget to set a password with ‘passwd’.
  users.users.hi = {
    isNormalUser = true;
    description = "hi";
    extraGroups = [ "networkmanager" "wheel" "vboxsf" "vboxusers"];
    shell = pkgs.zsh;
    packages = with pkgs; [
      firefox
      kate
      thunderbird
      chromium
    ];
  };

  # Allow unfree packages
  nixpkgs.config.allowUnfree = true;

  # virtualisation.virtualbox.guest.enable = true;
  # virtualisation.virtualbox.guest.x11 = true;
  # virtualisation.virtualbox.host.enable = true;
  # virtualisation.virtualbox.host.enableExtensionPack = true;

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  environment.systemPackages = with pkgs; [
  #  vim # Do not forget to add an editor to edit configuration.nix! The Nano editor is also installed by default.
  #  wget
    zsh
    zsh-autosuggestions
    zsh-syntax-highlighting
    zsh-powerlevel10k
    meslo-lgs-nf

    oh-my-zsh
    corefonts
    fira
    powerline-fonts
    ubuntu_font_family
    unifont

    p7zip
    unrar
    unzip

    util-linux
    meld

    home-manager

    git
    git-cola
    github-desktop

    emacs29

    autokey
    virtualbox
    librewolf
    palemoon-bin
    ventoy-full

    flatpak
    steam
    libsForQt5.falkon
    libsForQt5.korganizer
    krusader

    psensor
    playonlinux
    protonvpn-gui

    # vmware-workstation

    quickemu
    quickgui

    neofetch
  ];

  # Some programs need SUID wrappers, can be configured further or are   # started in user sessions.
  # programs.mtr.enable = true;
  # programs.gnupg.agent = {
  #   enable = true;
  #   enableSSHSupport = true;
  # };

  programs.zsh = {
    enable = true;
    enableCompletion = true;
    autosuggestions.enable = true;
    interactiveShellInit = ''
      source /run/current-system/sw/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
      source /run/current-system/sw/share/zsh-autosuggestions/zsh-autosuggestions.zsh
      source /run/current-system/sw/share/zsh-powerlevel10k/powerlevel10k.zsh-theme
    '';
  };

  fonts = {
    fontDir.enable = true;
    packages = with pkgs; [
      corefonts
      fira
      powerline-fonts
      ubuntu_font_family
      unifont
    ];
  };

  # List services that you want to enable:

   systemd.services.rfkill-block-all = {
     path = [ pkgs.networkmanager ];
     script = ''
       nmcli radio wifi off
     '';
     wantedBy = [ "multi-user.target" ];
   };

  # Enable the OpenSSH daemon.
  # services.openssh.enable = true;

  # Open ports in the firewall.
  # networking.firewall.allowedTCPPorts = [ ... ];
  # networking.firewall.allowedUDPPorts = [ ... ];
  # Or disable the firewall altogether.
  # networking.firewall.enable = false;

  networking.firewall.enable = true;
  powerManagement.enable = true;
  services.thermald.enable = true;

  # This value determines the NixOS release from which the default settings for stateful data, like file locations and database versions on your system were taken. It‘s perfectly fine and recommended to leave this value at the release version of the first install of this system. Before changing this value read the documentation for this option (e.g. man configuration.nix or on ... ).
  system.stateVersion = "23.11"; # Did you read the comment?
}

— Me@2024-01-23 09:55:24 AM

.

.

2024.02.29 Thursday (c) All rights reserved by ACHK

Euler problem 19.2

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

isLeap :: Int -> Bool
isLeap year
  = (mod year 4 == 0
     && mod year 100 /= 0)
  || mod year 400 == 0

daysInMonth :: Int -> Int -> Int
daysInMonth year month
  | month `elem` [1, 3, 5, 7, 8, 10, 12] = 31
  | month `elem` [4, 6, 9, 11] = 30
  | month == 2 = if isLeap year
                 then 29
                 else 28

nextMonth :: Int -> Int -> Int -> Int
nextMonth dayOfWeek y m 
  = (dayOfWeek + daysInMonth y m) `mod` 7

countSundays :: Int -> Int -> Int -> Int -> Int
countSundays startYear startMonth dayOfWeek sundays
  | y > 2000  = sundays
  | m > 12    = countSundays (y+1) 1 dayOfWeek sundays
  | otherwise = countSundays y (m+1) nextDayOfWeek sun
  where
    y = startYear
    m = startMonth
    nextDayOfWeek = nextMonth dayOfWeek y m 
    sun | dayOfWeek == 0 = sundays + 1
        | otherwise      = sundays

e19 :: Int
e19 = countSundays 1901 1 2 0
-- The third argument is dayOfWeek.
-- Its value is 2 here  
-- because 1 Jan 1901 was a Tuesday. 

λ> e19
171
λ> 

— Me@2024-02-10 12:00:39 PM

.

.

2024.02.11 Sunday (c) All rights reserved by ACHK

Euler problem 19.1

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

(defun is-leap-year (year)
  (or (and (zerop (mod year 4))
           (not (zerop (mod year 100))))
      (zerop (mod year 400))))

(defun days-in-month (month year)
  (case month
    ((1 3 5 7 8 10 12) 31)
    ((4 6 9 11) 30)
    (2 (if (is-leap-year year)
           29
           28))))

(defun euler-19 ()
  (let ((day-of-week 2) ; 1 Jan 1901 was a Tuesday
        (sundays 0))
    (loop :for year :from 1901 :to 2000 :do
      (loop :for month :from 1 :to 12 :do
        (when (zerop day-of-week) ; 0 represents Sunday
          (incf sundays))
        (setf day-of-week
              (mod
               (+ day-of-week
                  (days-in-month month year))
               7))))
    sundays))

(print (euler-19))

CL-USER> (euler-19)
171
CL-USER> 

— Me@2024-01-30 01:46:11 PM

.

.

2024.01.30 Tuesday (c) All rights reserved by ACHK

7z to encrypt folder with AES 256

Sequential speed, 2 | Restore, 2

.

7za a -mmt4 -tzip -p -mem=AES256 /target_folder/the_zip_file.zip /source_folder/

a

Add files to archive

-mmt[N]

set number of CPU threads

-t{Type}

Set type of archive

-p{Password}

set Password

-m{Parameters}

set compression Method

.

How to check the encryption algorithm of a 7z/zip file?

7z l -slt /target_folder/file.zip

l

List contents of archive

-slt

show technical information for l (List) command

.
To unzip:

7za x /source_folder/the_zip_file.7z -o/target_folder/

x

eXtract files with full paths

-o{Directory}

set Output directory

— Me@2023-11-26 12:17:46 PM

.

.

2024.01.14 Sunday (c) All rights reserved by ACHK

Euler problem 18.2

tri = [[75],
       [95,64],
       [17,47,82],
       [18,35,87,10],
       [20,04,82,47,65],
       [19,01,23,75,03,34],
       [88,02,77,73,07,63,67],
       [99,65,04,28,06,16,70,92],
       [41,41,26,56,83,40,80,70,33],
       [41,48,72,33,47,32,37,16,94,29],
       [53,71,44,65,25,43,91,52,97,51,14],
       [70,11,33,28,77,73,17,78,39,68,17,57],
       [91,71,52,38,17,14,91,43,58,50,27,29,48],
       [63,66,04,68,89,53,67,30,73,16,69,87,40,31],
       [04,62,98,27,23,09,70,98,73,93,38,53,60,04,23]]
  
pathAddItem x (y:ys) (z:zs)
  = (x+m):x:ms
  where 
    (m:ms) | y > z = y:ys
           | otherwise = z:zs
           
listApplyPathAddItem xs ys
  = zipWith3 f xs ys $ tail ys 
  where
    f = pathAddItem
   
e18 = foldr g acc (init tri) 
  where
    toPair x = [x,x]
    g = listApplyPathAddItem
    acc = map toPair $ last tri

— Me@2023-12-28 11:25:22 AM

.

.

2023.12.28 Thursday (c) All rights reserved by ACHK

Euler problem 18.1

(ql:quickload "str")

;; (defun file-get-contents (filename)
;;   (with-open-file (stream filename)
;;     (let ((contents (make-string
;;                      (file-length stream))))
;;       (read-sequence contents stream)
;;       contents)))

(defun file-get-lines (filename)
  (with-open-file (stream filename)
    (loop :for line = (read-line stream nil)
          :while line
          :collect line)))

;; (defun string-to-list (the-string)
;;   (loop :for char :across the-string
;;         :collect char))

;; (defun char-to-integer-list (char-list)
;;   (mapcar #'digit-char-p char-list))

(defun path-add-item (x yys zzs)
  (let* ((y (car yys))
         (ys (cdr yys))
         (z (car zzs))
         (zs (cdr zzs))
         (m (if (> y z) y z))
         (ms (if (> y z) ys zs)))
    (cons (+ x m) (cons x ms))))

(defmacro zipWith3 (f xs ys zs)
  `(mapcar ,f ,xs ,ys ,zs))

(defun path-add-item-zip (acc xs)
  (zipWith3 #'path-add-item xs acc (cdr acc)))

(defun foldl (f acc xs)
  (if (not xs)
      acc
      (foldl f (funcall f acc (car xs)) (cdr xs))))

(defun string-split (x)
  (str:split #\space x))

(defun map-parse-integer (xs)
  (mapcar #'parse-integer xs))

(defmacro map-string-split (xs)
  `(mapcar #'string-split ,xs))

(defmacro head (xs)
  `(car ,xs))

(defmacro tail (xs)
  `(cdr ,xs))

(defun pair-double (x)
  (list x x))

(defun e18 ()
  (let* ((string-lines (file-get-lines #P"n.txt"))
         (string-lists (map-string-split string-lines))
         (number-tower (mapcar #'map-parse-integer
                               string-lists))
         (r-tower (reverse number-tower))
         (acc (mapcar #'pair-double (head r-tower))))
    
    (foldl #'path-add-item-zip acc (tail r-tower))))

CL-USER> (e18)
((1074 75 64 82 87 82 75 73 28 83 32 91 78 58 73 93))
CL-USER> 

— Me@2023-12-20 07:32:00 PM

.

.

2023.12.21 Thursday (c) All rights reserved by ACHK

Eval the buffer

(defun common-lisp-eval (str)
  (unless (slime-current-connection)
    (let ((wnd (current-window-configuration)))
      (slime)
      (while (not (and (slime-current-connection)
                       (get-buffer-window
                        (slime-output-buffer))))
        (sit-for 0.2))
      (set-window-configuration wnd)))
  (let (deactivate-mark)
    (slime-eval `(swank:eval-and-grab-output ,str))))

(defun file-eval ()
  (interactive)
  (message
   (car (common-lisp-eval
         (concat "(format t \"~{~a~%~}\" (list "
                 (string-make-unibyte
                  (buffer-string)) "))")))))

(defun region-eval (from to)
  (interactive "r")
  (message
   (car (common-lisp-eval
         (concat "(format t \"~{~a~%~}\" (list "
                 (string-make-unibyte
                  (buffer-substring
                   from to)) "))")))))

(global-set-key (kbd "C-F") 'file-eval)
(global-set-key (kbd "C-R") 'region-eval)

— Me@2023-12-10 09:59:57 AM

.

.

2023.12.11 Monday (c) All rights reserved by ACHK

Quicklisp error

Common Lisp calculator, 2

.

1. To run the following code, you have to install the Quicklisp library manager by following the steps in my blog post titled cmu-infix.

.

2.1 To enable the library, in the Common Lisp REPL, run the code:

(ql:quickload :cmu-infix)

(named-readtables:in-readtable cmu-infix:syntax)

2.2 If you encounter the error message:

Lisp connection closed unexpectedly: connection broken by remote peer

2.2.1 empty the cache folder:

~/.cache/common-lisp/

2.2.2 Restart the Common Lisp REPL.

2.2.3 Reload the library.

.

3.1 The goal of the following code is to create our own REPL.

(defmacro infix-string-eval (a-string)
  `(eval (read-from-string
          (concatenate 'string
                       "#I (" ,a-string ")"))))

(defun read-repl-all ()
  (loop (print (write-to-string (read)))))

(defun my-repl ()
  (loop
    (let ((a-string (write-to-string (read))))
      (if (or (string-equal "(exit)" a-string)
              (string-equal "exit" a-string))
          (return)            
          (print
           (infix-string-eval
            (string-trim "|()" a-string))))))) 

3.2 In the Common Lisp REPL, run the code:

(my-repl)

— Me@2022-12-26 01:10:34 PM

.

The symbol

^

is the bitwise exclusive OR.

To create an exponentiation, we use

^^

— Me@2023-09-09 12:51:46 PM

.

— Me@2023-11-22 12:56:00 PM

.

.

2023.11.23 Thursday (c) All rights reserved by ACHK

Euler problem 17.2

import Data.Char ( digitToInt )

one :: [String]
one =
  [ "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",
    "twelve",
    "thirteen",
    "fourteen",
    "fifteen",
    "sixteen",
    "seventeen",
    "eighteen",
    "nineteen"
  ]

ty :: [String]
ty =
  [ "twenty",
    "thirty",
    "forty",
    "fifty",
    "sixty",
    "seventy",
    "eighty",
    "ninety"
  ]

english :: Int -> [Char]
english x
  | x == 0 = []
  | x < 20 = one !! (x - 1)
  | x >= 20 && x < 100 =
      ty !! (firstDigit x - 2)
        ++ " "
        ++ english (x - firstDigit x * 10)
        
  | x < 1000 && x `mod` 100 == 0 =
      one !! (firstDigit x - 1)
        ++ " hundred"
      
  | x > 100 && x <= 999 =
      one !! (firstDigit x - 1)
        ++ " hundred and "
        ++ english (x - firstDigit x * 100)
        
  | x == 1000 = "one thousand"
  | otherwise = "error"
  where
    firstDigit = digitToInt . head . show

removeSpace :: [Char] -> [Char]
removeSpace = filter (`notElem` " ")

engCat :: [Int] -> [Char]
engCat = concatMap english

e17 :: Int
e17 = length . removeSpace $ engCat [1..1000]

— based on Haskell official

λ> e17
21124

— colorized by palette fm

— Me@2023-11-07 11:24:03 PM

.

.

2023.11.08 Wednesday (c) All rights reserved by ACHK

Euler problem 17.1

(defun remove-char (lst string)
  (cond ((not lst) string)
        ('t (remove-char (cdr lst)
                         (remove (car lst)
                                 string)))))

(defmacro english-reduced (n)
  `(length (remove-char (list #\- #\space)
                        (format nil "~r" ,n))))

(defmacro english-and (n)
  `(cond
     ((= (mod n 100) 0) (english-reduced ,n))
     ((> ,n 100) (+ 3 (english-reduced ,n)))
     ('t (english-reduced ,n))))

(defun e17 ()
  (labels ((e17-iter (n acc)
             (cond ((<= n 0) acc)
                   ('t (e17-iter (- n 1)
                                 (+ acc
                                    (english-and n)))
                       ))))
    (e17-iter 1000 0)))

CL-USER> (e17)
21124

— Me@2023-10-26 06:36:38 PM

.

.

2023.10.26 Thursday (c) All rights reserved by ACHK

Picasa

This is a file from the Wikimedia Commons.

Picasa was a cross-platform image organizer and image viewer for organizing and editing digital photos, integrated with a now defunct photo-sharing website, originally created by a company named Lifescape (which at that time was incubated by Idealab) in 2002.

— Wikipedia on Picasa

— Me@2023-10-10 08:09:55 PM

.

.

2023.10.10 Tuesday (c) All rights reserved by ACHK

Euler problem 16.1

(defun digits (n)
  (labels
      ((f-iter (m acc)
         (cond ((= m 0) acc)
               ('t (f-iter (floor m 10)
                           (cons (mod m 10) acc))))))
    (f-iter n NIL)))

(defmacro sum (lst)
  `(reduce #'+ ,lst))

(sum (digits (expt 2 1000)))

— Me@2023-09-17 01:04:49 AM

.

.

2023.09.17 Sunday (c) All rights reserved by ACHK

Common Lisp calculator, 1

To run the following code, you have to install the Quicklisp library manager by following the steps in my blog post titled cmu-infix.

(ql:quickload :infix-math)

(asdf:load-system "infix-math/calc")

(infix-math/calc:calc)

This problem of using this calculator is that spaces are required both before and after an operator:

wrong:

1+1

correct:

1 + 1

— Me@2022-12-26 10:19:05 AM

.

.

2023.09.09 Saturday (c) All rights reserved by ACHK

Standard Model equation

Euler problem 15.2

.

factorial n = facIter n 1
  where
    facIter m acc =
      if m == 0
      then acc
      else facIter (m - 1) (m * acc)

binomial n r = bIter n 1 `div` factorial r 
  where
    bIter m acc = 
      if m < n - r + 1
      then acc
      else bIter (m - 1) (m * acc)

f n = product [1..n]

b n r = product [(n-r+1)..n] `div` f r

— Me@2023-09-06 08:08:35 PM

.

.

2023.09.06 Wednesday (c) All rights reserved by ACHK

Euler problem 15.1

— Poorly Drawn Lines

.

(defun factorial (n)
  (labels
      ((f-iter (m acc)
         (cond ((= m 0) acc)
               ('t (f-iter (1- m)
                           (* m acc))))))
    (f-iter n 1)))

(defun binomial (n r)
  (labels
      ((b-iter (m acc)
         (cond ((< m (1+ (- n r))) acc)
               ('t (b-iter (1- m)
                           (* m acc))))))
    (/ (b-iter n 1) (factorial r))))

.

— Me@2023-08-26 11:27:59 AM

.

.

2023.08.27 Sunday (c) All rights reserved by ACHK

Sequential speed



--recurse-paths
-r

recurse into directories


--symlinks
-y

store symbolic links as the link instead of the referenced file

.

zip -r -y /dest_folder/the_zip_file.zip /source_folder/

source_folder is the folder to be zipped.

dest_folder is the destination folder for storing the zip file.

After zipping, the resulting file is the_zip_file.zip, which is a zipped version of the source_folder.

— Me@2023-08-03 09:41:29 AM

.

.

2023.08.07 Monday (c) All rights reserved by ACHK

Euler problem 14.2

import Data.Array ( listArray, (!) )
import Data.List ( elemIndex )

collatzLengthList m = collatzLengthArray
  where
    collatzLengthArray
      = listArray (1,m) $ 1:map collatzLength [2..m]
      where
        collatzLength n
          | b<=m = 1 + collatzLengthArray ! b
          | otherwise = 1 + collatzLength b
          where
            b
              | even n = n `div` 2 
              | otherwise = 3*n+1

e14 :: (Maybe Int, Integer)
e14 = (n, m)
  where
    m = maximum cArray
    cArray = collatzLengthList p
    p = 1000000
    n = elemIndex m cList
      where
        cList = 0:[cArray ! x | x <- [1..p]]

λ> :set +s
λ> e14
(Just 837799,525)
(1.74 secs, 2,193,219,920 bytes)

— Me@2023-07-25 11:55:41 PM

.

.

2023.07.26 Wednesday (c) All rights reserved by ACHK

Memoize

Euler problem 14.1.2

.

(defun range (max &key (min 0) (step 1))
  (loop :for n :from min :below max :by step
        collect n))

(defun max-item (lst)
  (loop :for item :in lst
        :maximize item))

(defmacro r-nth (n lst)
  `(nth (- (length ,lst) ,n) ,lst))

(defun collatz-length (n c)
  (cond ((= n 1) c) 
        ((evenp n) (collatz-length (/ n 2) (1+ c)))
        ('t (collatz-length (1+ (* 3 n)) (1+ c)))))

(defun memoize (fn)
  (let ((cache (make-hash-table :test #'equal)))
    #'(lambda (&rest args)
        (multiple-value-bind (val win)
            (gethash args cache)
          (if win
              val
              (setf (gethash args cache)
                    (apply fn args)))))))

(setf (fdefinition 'collatz-length)
      (memoize #'collatz-length))

(time (max-item (mapcar #'(lambda (x)
                            (collatz-length x 1))
                        (range 1000001 :min 1))))

;; Heap exhausted (no more space for allocation).
;; 109314048 bytes available, 171127632 requested.

;; PROCEED WITH CAUTION.
;;    [Condition of type
;;       SB-KERNEL::HEAP-EXHAUSTED-ERROR]

(time (max-item (mapcar #'(lambda (x)
                            (collatz-length x 1))
                        (range 250001 :min 1))))

;; Evaluation took:
;; 1.380 seconds of real time

(time (max-item (mapcar #'(lambda (x)
                            (collatz-length x 1))
                        (range 250001 :min 1))))

;; Evaluation took:
;; 0.068 seconds of real time

— Me@2023-07-14 02:54:38 PM

.

.

2023.07.14 Friday (c) All rights reserved by ACHK